-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderScript.php
350 lines (298 loc) · 13.5 KB
/
orderScript.php
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
<?php
/*
* Order Migration Script -Magento 2
* Using Mofluid API
* */
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Including Bootstrap
useMagentoFrameworkAppBootstrap;
include ('../app/bootstrap.php');
$mage_bootstrap = Bootstrap::create(BP, $_SERVER);
// Getting Instance of Object Manager
$objectManager = $mage_bootstrap->getObjectManager();
// Log File to maitain any sort of Exceptions
$log_file_order = 'order_log.txt';
$log_order_prod = 'order_product.txt';
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Dynamic Range for Script to run from Browser
$limit = $_GET['limit'];
$offset = $_GET['offset'];
/*
* Drupal Connection
* Update dbname, Username, Password a/c to Drupal Database
* */
$connection = $objectManager->create('\Magento\Framework\App\ResourceConnection\ConnectionFactory')->create(array(
'host' => 'localhost',
'dbname' => 'ABCDE',
'username' => 'root',
'password' => 'abc@123',
'active' => '1',
));
/*
* Magento Object Connection
* */
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection_magento = $resource->getConnection();
// Connection Object
$context = $objectManager->get('\Magento\Framework\App\Helper\Context');
$scopeConfig = $context->getScopeConfig();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$productFactory = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$quoteManagement = $objectManager->get('\Magento\Quote\Model\QuoteManagement');
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$customerRepository = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface');
$orderService = $objectManager->get('\Magento\Sales\Model\Service\OrderService');
$cartRepositoryInterface = $objectManager->get('\Magento\Quote\Api\CartRepositoryInterface');
$cartManagementInterface = $objectManager->get('\Magento\Quote\Api\CartManagementInterface');
$shippingRate = $objectManager->get('\Magento\Quote\Model\Quote\Address\Rate');
$region = $objectManager->get('\Magento\Directory\Model\Region');
// Mofluid Api Relation Object
$helper = $objectManager->create('Mofluid\Mofluidapi2\Helper\Data');
// Loading Order related data from Drupal
$sql_uc_orders = "SELECT * FROM `uc_orders` ORDER BY `order_id` ASC limit {$limit} offset {$offset}";
$result_uc_orders = $connection->fetchAll($sql_uc_orders); // uc_orders
$c = 1;
foreach($result_uc_orders as $value_uc_order)
{
$sql_admin_comment = "SELECT * FROM `uc_order_admin_comments` WHERE order_id={$value_uc_order['order_id']}"; //Order Admin Comment
$result_admin_comment = $connection->fetchAll($sql_admin_comment);
$sql_order_comment = "SELECT * FROM `uc_order_comments` WHERE order_id={$value_uc_order['order_id']}"; //Order Comment
$result_order_comment = $connection->fetchAll($sql_order_comment);
$sql_order_log = "SELECT * FROM `uc_order_log` WHERE `order_id` ={$value_uc_order['order_id']}"; //Order Log
$result_order_log = $connection->fetchAll($sql_order_log);
$sql_order_products = "SELECT * FROM `uc_order_products` WHERE `order_id` = {$value_uc_order['order_id']}"; //Products details
$result_order_products = $connection->fetchAll($sql_order_products);
$sql_order_quote = "SELECT * FROM `uc_order_quotes` WHERE `order_id` = {$value_uc_order['order_id']}";
$result_order_quote = $connection->fetchAll($sql_order_quote);
$sql_order_transaction = "SELECT * FROM `uc_payment_paypal_ipn` WHERE `order_id` = {$value_uc_order['order_id']}"; //transaction Id
$result_order_transaction = $connection->fetchAll($sql_order_transaction);
// ----------------------------magento product ---------------------------//
$result_prod_magento = array();
foreach($result_order_products as $order_products)
{
$sql_prod_magento = "SELECT * FROM `catalog_product_entity` WHERE `sku` = '{$order_products['model']}'";
$result_prod_magento = $connection_magento->fetchAll($sql_prod_magento);
}
/*Validating If Order Product Exist in Magento 2*/
if (count($result_prod_magento, 1) == 0)
{
file_put_contents($log_order_prod, $order_products['model'] . '--->' . $value_uc_order['order_id'] . "\n", FILE_APPEND);
continue;
}
// Converting in Base64 for Mofluid API
$myJSON_product = json_encode($result_prod_magento);
$decodedtext_product = base64_encode($myJSON_product);
// Updating country and region code of Drupal with corresponding Magento Code
$sql_country_id = "SELECT country_iso_code_2 FROM `uc_countries` WHERE `country_id` ={$value_uc_order['delivery_country']}";
$result_country_id = $connection->fetchAll($sql_country_id);
$sql_country_billing_id = "SELECT country_iso_code_2 FROM `uc_countries` WHERE `country_id` ={$value_uc_order['billing_country']}";
$result_country_billing_id = $connection->fetchAll($sql_country_billing_id);
// Unix Time Conversion
$time_created = date("Y-m-d h:i:s A T", $value_uc_order['created']);
$time_updated = date("Y-m-d h:i:s A T", $value_uc_order['modified']);
//-------------------Shipping & Billing Addresss---------------------------//
$orderData_address=[
'shipping_address' =>[
'firstname' => "{$value_uc_order['delivery_first_name']}",
'lastname' => "{$value_uc_order['delivery_last_name']}",
'email' =>"{$value_uc_order['primary_email']}",
'street' => "{$value_uc_order['delivery_street1']}".''."{$value_uc_order['delivery_street2']}",
'city' => "{$value_uc_order['delivery_city']}",
'country_id' => "{$result_country_id[0]['country_iso_code_2']}",
'region' => "{$value_uc_order['delivery_zone']}",
'postcode' => "{$value_uc_order['delivery_postal_code']}",
'telephone' => "{$value_uc_order['delivery_phone']}",
'fax' => null,
'company' =>"{$value_uc_order['delivery_company']}",
'save_in_address_book' => 1
],
'billing_address' =>[
'firstname' => "{$value_uc_order['delivery_first_name']}",
'lastname' => "{$value_uc_order['delivery_last_name']}",
'email' =>"{$value_uc_order['primary_email']}",
'street' => "{$value_uc_order['delivery_street1']}".''."{$value_uc_order['delivery_street2']}",
'city' => "{$value_uc_order['delivery_city']}",
'country_id' => "{$result_country_billing_id[0]['country_iso_code_2']}",
'region' => "{$value_uc_order['delivery_zone']}",
'postcode' => "{$value_uc_order['delivery_postal_code']}",
'telephone' => "{$value_uc_order['delivery_phone']}",
'fax' => null,
'company' =>"{$value_uc_order['delivery_company']}",
'save_in_address_book' => 1
],
];
// Converting in Base64 for Mofluid API
$myJSON_address = json_encode($orderData_address);
$decodedtext_address = base64_encode($myJSON_address);
// Creating Array of Transaction ID
$trans_id = array();
$trans_mc_gross = array();
if (count($result_order_transaction, 1) >= 1)
{
$i = 'a';
foreach($result_order_transaction as $order_trans)
{
$trans_id[$i] = $order_trans['txn_id'];
$trans_mc_gross[$i] = $order_trans['mc_gross'];
$i++;
}
}
else
{
$trans_id = array(
'a' => 123456789
);
$trans_mc_gross = array(
'a' => 0
);
}
// Converting in Base64 for Mofluid API
$encoded_trans_id = json_encode($trans_id, true);
$encoded_trans_mc_gross = json_encode($trans_mc_gross, true);
// --------------Calling Placeorder function of Mofluid---------------------//
if ($value_uc_order['order_status'] == 'completed')
{
$value_uc_order['order_status'] = 'complete';
}
$sql_user_magento = "SELECT customer_id from user_relation WHERE drupal_customer_id={$value_uc_order['uid']}";
$result_user_magento = $connection_magento->fetchAll($sql_user_magento);
if (count($result_user_magento, 1) == 0)
{
/*
* GUEST Order Creation
* */
file_put_contents($log_file_order, $value_uc_order['order_id'] . '--->' . $value_uc_order['uid'] . '--->' . 'guest' . "\n", FILE_APPEND);
try
{
/*
* CURL request to hit Mofluid API to create Order
* */
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => 'http://<YOUR URL>/mofluidapi2?callback=&',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'store' => 1,
'currency' => 'USD',
'service' => 'placeorder',
'customerid' => 0,
'products' => $decodedtext_product,
'address' => $decodedtext_address,
'is_create_quote' => 1,
'shipmethod' => 'freeshipping_freeshipping',
'paymentmethod' => 'cashondelivery',
'couponCode' => 'null',
'transactionid' => $encoded_trans_id,
'order_status' => $value_uc_order['order_status'],
'trans_gross' => $encoded_trans_mc_gross
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
$index = json_decode($result, true);
}
catch(Exception $e)
{
echo 'Message:' . $e->getMessage();
}
}
else
{
/*
* Registered Customer Order Creation
* */
try
{
/*
* CURL request to hit Mofluid API to create Order
* */
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => 'http://<YOUR URL>/mofluidapi2?callback=&',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'store' => 1,
'currency' => 'USD',
'service' => 'placeorder',
'customerid' => $result_user_magento[0]['customer_id'],
'products' => $decodedtext_product,
'address' => $decodedtext_address,
'is_create_quote' => 1,
'shipmethod' => 'freeshipping_freeshipping',
'paymentmethod' => 'cashondelivery',
'couponCode' => 'null',
'transactionid' => $encoded_trans_id,
'order_status' => $value_uc_order['order_status'],
'trans_gross' => $encoded_trans_mc_gross
)
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
$index = json_decode($result, true);
}
catch(Exception $e)
{
echo 'Message:' . $e->getMessage();
}
}
// -------------------Order Relation Table Maintain------------------------------------//
$sql_relation = "INSERT INTO `order_relation`(`drupal_order_id`, `magento_order_id`) VALUES ({$value_uc_order['order_id']}, {$index['a']})";
$connection_magento->query($sql_relation);
// ---------------------Update for transaction Data------------------------------------//
if ($trans_id['a'] != 123456789)
{
foreach($result_order_transaction as $order_trans)
{
$order_trans['received'] = date("Y-m-d h:i:s A T", $order_trans['received']);
$sql_update_trans_time = "UPDATE `sales_payment_transaction` SET `created_at`='{$order_trans['received']}', `txn_type`='{$order_trans['txn_type']}', `receiver_email`='{$order_trans['receiver_email']}', `payer_email`='{$order_trans['payer_email']}' WHERE txn_id ='{$order_trans['txn_id']}' AND order_id = {$index['a']}";
$connection_magento->query($sql_update_trans_time);
}
}
else
{
$sql_update_trans = "DELETE FROM `sales_payment_transaction` WHERE txn_id ='123456789' AND order_id = {$index['a']} ";
$connection_magento->query($sql_update_trans);
}
// ----------------Update of Invoice Date---------------------------------------//
$sql_invoice = "UPDATE `sales_invoice_grid` SET `created_at`='{$time_updated}', `updated_at`='{$time_updated}', `order_created_at`='{$time_updated}' WHERE order_id={$index['a']}";
$connection_magento->query($sql_invoice);
// -----------------------Order Creation Time Updation------------------------//
$sql = "UPDATE `sales_order` SET `created_at`='{$time_created}', `updated_at`='{$time_updated}', `shipping_method`='freeshipping_freeshipping' WHERE entity_id={$index['a']}";
$connection_magento->query($sql);
$sql_order_grid = "UPDATE `sales_order_grid` SET `created_at`='{$time_created}', `updated_at`='{$time_updated}' WHERE entity_id={$index['a']}";
$connection_magento->query($sql_order_grid);
// ----------------Order Related Comments-----------------//
foreach($result_admin_comment as $admin_comment)
{
$admin_comment['created'] = date("Y-m-d h:i:s A T", $admin_comment['created']);
createComment($admin_comment['order_id'], $admin_comment['message'], null, $admin_comment['created'], "By_admin", $index['a']);
}
foreach($result_order_comment as $order_comment)
{
$order_comment['created'] = date("Y-m-d h:i:s A T", $order_comment['created']);
createComment($order_comment['order_id'], $order_comment['message'], null, $order_comment['created'], "order_comment", $index['a']);
}
foreach($result_order_log as $order_log)
{
$split = explode(" ", $order_log['changes']);
$order_log['changes'] = $split[count($split) - 1];
$order_log['created'] = date("Y-m-d h:i:s A T", $order_log['created']);
createComment($order_log['order_id'], $order_log['changes'], null, $order_log['created'], "order_log", $index['a']);
}
print_r("Order Created");
}
// ----------Function to CreateComment-----------------------------//
function createComment($order_no, $comment, $status, $created, $entity, $ind)
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection_magento = $resource->getConnection();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($ind);
$order->addStatusHistoryComment(strip_tags($comment))->setIsVisibleOnFront(true)->setIsCustomerNotified(false)->setStatus(strip_tags($status))->setCreatedAt($created)->setShowswhatentityhistoryisbindto($entity)->save();
}
?>