codistoconnect - Version 1.90.27

Version Notes

Release 1.90.27

Download this release

Release Info

Developer Codisto
Extension codistoconnect
Version 1.90.27
Comparing to
See all releases


Code changes from version 1.90.26 to 1.90.27

app/code/community/Codisto/Sync/controllers/IndexController.php.save ADDED
@@ -0,0 +1,1991 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Codisto eBay Sync Extension
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Open Software License (OSL 3.0)
8
+ * that is bundled with this package in the file LICENSE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://opensource.org/licenses/osl-3.0.php
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * @category Codisto
16
+ * @package Codisto_Sync
17
+ * @copyright Copyright (c) 2015 On Technology Pty. Ltd. (http://codisto.com/)
18
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
19
+ */
20
+
21
+ class Codisto_Sync_IndexController extends Mage_Core_Controller_Front_Action
22
+ {
23
+ public function calcAction()
24
+ {
25
+ set_time_limit(0);
26
+ ignore_user_abort(false);
27
+
28
+ $output = '';
29
+
30
+ try
31
+ {
32
+ $request = $this->getRequest();
33
+ $response = $this->getResponse();
34
+
35
+ $storeId = $request->getQuery('storeid') == null ? 0 : (int)$request->getQuery('storeid');
36
+
37
+ if($storeId == 0)
38
+ {
39
+ // jump the storeid to first non admin store
40
+ $stores = Mage::getModel('core/store')->getCollection()
41
+ ->addFieldToFilter('is_active', array('neq' => 0))
42
+ ->addFieldToFilter('store_id', array('gt' => 0))
43
+ ->setOrder('store_id', 'ASC');
44
+
45
+ $firstStore = $stores->getFirstItem();
46
+ if(is_object($firstStore) && $firstStore->getId())
47
+ $storeId = $firstStore->getId();
48
+ }
49
+
50
+ $store = Mage::app()->getStore($storeId);
51
+
52
+ $currencyCode = $request->getPost('CURRENCY');
53
+ if(!$currencyCode)
54
+ $currencyCode = $store->getCurrentCurrencyCode();
55
+
56
+ $place = $request->getPost('PLACE');
57
+ if(!$place)
58
+ $place = '';
59
+ $postalcode = $request->getPost('POSTALCODE');
60
+ $division = $request->getPost('DIVISION');
61
+ $countrycode = $request->getPost('COUNTRYCODE');
62
+ $regionid = null;
63
+ $regioncode = null;
64
+
65
+ if($countrycode == 'AU')
66
+ {
67
+ $pc = $postalcode{0};
68
+
69
+ if ($pc == 2 || $pc == 1) {
70
+ $regiontext = 'NSW';
71
+ } else if ($pc == 3 || $pc == 8) {
72
+ $regiontext = 'VIC';
73
+ } else if ($pc == 4) {
74
+ $regiontext = 'QLD';
75
+ } else if ($pc == 5) {
76
+ $regiontext = 'SA';
77
+ } else if ($pc == 6) {
78
+ $regiontext = 'WA';
79
+ } else if ($pc == 7) {
80
+ $regiontext = 'TAS';
81
+ }
82
+
83
+ $pc3 = $postalcode{0} . $postalcode{1};
84
+ if ($pc3 == '08' || $pc3 == '09') {
85
+ $regiontext = 'NT';
86
+ }
87
+
88
+ if ($postalcode == '0872') {
89
+ $regiontext = 'SA';
90
+ } else if ($postalcode == '2611' || $postalcode == '3500' || $postalcode == '3585' || $postalcode == '3586' || $postalcode == '3644' || $postalcode == '3707') {
91
+ $regiontext = 'NSW';
92
+ } else if ($postalcode == '2620') {
93
+ $regiontext = 'ACT';
94
+ }
95
+
96
+ if (intval($postalcode) >= 2600 && intval($postalcode) <= 2618) {
97
+ $regiontext = 'ACT';
98
+ }
99
+
100
+ $region = Mage::getModel('directory/region')->loadByCode($regiontext, $countrycode);
101
+ if($region)
102
+ {
103
+ $regionid = $region->getId();
104
+ $regioncode = $region->getCode();
105
+ }
106
+ }
107
+ else
108
+ {
109
+ $region = Mage::getModel('directory/region')->loadByName($division, $countrycode);
110
+ if($region)
111
+ {
112
+ $regionid = $region->getId();
113
+ $regioncode = $region->getCode();
114
+ }
115
+ }
116
+
117
+ $total = 0;
118
+ $itemqty = 0;
119
+ $totalweight = 0;
120
+
121
+ $quote = Mage::getModel('sales/quote');
122
+ $quote->setIsSuperMode(true);
123
+
124
+ for($inputidx = 0; ; $inputidx++)
125
+ {
126
+ if(!$request->getPost('PRODUCTCODE('.$inputidx.')'))
127
+ break;
128
+
129
+ $productid = (int)$request->getPost('PRODUCTID('.$inputidx.')');
130
+ if(!$productid)
131
+ {
132
+ $productcode = $request->getPost('PRODUCTCODE('.$inputidx.')');
133
+ $productid = Mage::getModel('catalog/product')->getIdBySku($productcode);
134
+ }
135
+ else
136
+ {
137
+ $sku = Mage::getResourceSingleton('catalog/product')->getProductsSku(array($productid));
138
+ if(empty($sku))
139
+ {
140
+ $productcode = $request->getPost('PRODUCTCODE('.$inputidx.')');
141
+ $productid = Mage::getModel('catalog/product')->getIdBySku($productcode);
142
+ }
143
+ }
144
+
145
+ $productqty = $request->getPost('PRODUCTQUANTITY('.$inputidx.')');
146
+ if(!$productqty && $productqty !=0)
147
+ $productqty = 1;
148
+
149
+ $productprice = floatval($request->getPost('PRODUCTPRICE('.$inputidx.')'));
150
+ $productpriceincltax = floatval($request->getPost('PRODUCTPRICEINCTAX('.$inputidx.')'));
151
+ $producttax = floatval($request->getPost('PRODUCTTAX('.$inputidx.')'));
152
+
153
+ if($productid)
154
+ {
155
+ $product = Mage::getModel('catalog/product')->load($productid);
156
+
157
+ if($product)
158
+ {
159
+ $product->setIsSuperMode(true);
160
+
161
+ $taxpercent = $productprice == 0 ? 0 : round($productpriceincltax / $productprice - 1.0, 2) * 100;
162
+
163
+ $item = Mage::getModel('sales/quote_item');
164
+ $item->setStoreId($store->getId());
165
+ $item->setQuote($quote);
166
+
167
+ $item->setData('product', $product);
168
+ $item->setProductId($productid);
169
+ $item->setProductType('simple');
170
+ $item->setIsRecurring(false);
171
+ $item->setTaxClassId($product->getTaxClassId());
172
+ $item->setBaseCost($product->getCost());
173
+ $item->setSku($product->getSku());
174
+ $item->setName($product->getName());
175
+ $item->setIsVirtual(0);
176
+ $item->setIsQtyDecimal(0);
177
+ $item->setNoDiscount(true);
178
+ $item->setWeight($product->getWeight());
179
+ $item->setData('qty', $productqty);
180
+ $item->setPrice($productprice);
181
+ $item->setBasePrice($productprice);
182
+ $item->setCustomPrice($productprice);
183
+ $item->setDiscountPercent(0);
184
+ $item->setDiscountAmount(0);
185
+ $item->setBaseDiscountAmount(0);
186
+ $item->setTaxPercent($taxpercent);
187
+ $item->setTaxAmount($producttax);
188
+ $item->setBaseTaxAmount($producttax);
189
+ $item->setRowTotal($productprice * $productqty);
190
+ $item->setBaseRowTotal($productprice * $productqty);
191
+ $item->setRowTotalWithDiscount($productprice * $productqty);
192
+ $item->setRowWeight($product->getWeight() * $productqty);
193
+ $item->setOriginalCustomPrice($productprice);
194
+ $item->setPriceInclTax($productpriceincltax);
195
+ $item->setBasePriceInclTax($productpriceincltax);
196
+ $item->setRowTotalInclTax($productpriceincltax * $productqty);
197
+ $item->setBaseRowTotalInclTax($productpriceincltax * $productqty);
198
+ $item->setWeeeTaxApplied(serialize(array()));
199
+
200
+ $total += $productpriceincltax * $productqty;
201
+ $itemqty += $productqty;
202
+ $totalweight += $product->getWeight() * $productqty;
203
+
204
+ $quote->getItemsCollection()->addItem($item);
205
+ }
206
+ }
207
+ }
208
+
209
+ $quote->save();
210
+
211
+ $checkoutSession = Mage::getSingleton('checkout/session');
212
+ $checkoutSession->replaceQuote($quote);
213
+ $checkoutSession->setData('destination_type', 'residence');
214
+
215
+ $currency = Mage::getModel('directory/currency')->load($currencyCode);
216
+
217
+ $shippingRequest = Mage::getModel('shipping/rate_request');
218
+ $shippingRequest->setAllItems($quote->getAllItems());
219
+ $shippingRequest->setDestCountryId($countrycode);
220
+ if($regionid)
221
+ $shippingRequest->setDestRegionId($regionid);
222
+ if($regioncode)
223
+ $shippingRequest->setDestRegionCode($regioncode);
224
+ if($place)
225
+ $shippingRequest->setDestCity($place);
226
+ $shippingRequest->setDestPostcode($postalcode);
227
+ $shippingRequest->setPackageValue($total);
228
+ $shippingRequest->setPackageValueWithDiscount($total);
229
+ $shippingRequest->setPackageWeight($totalweight);
230
+ $shippingRequest->setPackageQty($itemqty);
231
+ $shippingRequest->setPackagePhysicalValue($total);
232
+ $shippingRequest->setFreeMethodWeight(0);
233
+ $shippingRequest->setStoreId($store->getId());
234
+ $shippingRequest->setWebsiteId($store->getWebsiteId());
235
+ $shippingRequest->setFreeShipping(0);
236
+ $shippingRequest->setBaseCurrency($currency);
237
+ $shippingRequest->setPackageCurrency($currency);
238
+ $shippingRequest->setBaseSubtotalInclTax($total);
239
+
240
+ $shippingResult = Mage::getModel('shipping/shipping')->collectRates($shippingRequest)->getResult();
241
+
242
+ $shippingRates = $shippingResult->getAllRates();
243
+
244
+ $outputidx = 0;
245
+ foreach($shippingRates as $shippingRate)
246
+ {
247
+ if($shippingRate instanceof Mage_Shipping_Model_Rate_Result_Method)
248
+ {
249
+ $isPickup = $shippingRate->getPrice() == 0 &&
250
+ (preg_match('/(?:^|\W|_)pick\s*up(?:\W|_|$)/i', strval($shippingRate->getMethod())) ||
251
+ preg_match('/(?:^|\W|_)pick\s*up(?:\W|_|$)/i', strval($shippingRate->getCarrierTitle())) ||
252
+ preg_match('/(?:^|\W|_)pick\s*up(?:\W|_|$)/i', strval($shippingRate->getMethodTitle())));
253
+
254
+ if(!$isPickup)
255
+ {
256
+ $output .= 'FREIGHTNAME('.$outputidx.')='.rawurlencode($shippingRate->getMethodTitle()).'&FREIGHTCHARGEINCTAX('.$outputidx.')='.$shippingRate->getPrice().'&';
257
+ $outputidx++;
258
+ }
259
+ }
260
+ }
261
+
262
+ try
263
+ {
264
+ $quote
265
+ ->setIsActive(false)
266
+ ->delete();
267
+ }
268
+ catch(Exception $e)
269
+ {
270
+
271
+ }
272
+
273
+ }
274
+ catch(Exception $e)
275
+ {
276
+
277
+ }
278
+
279
+ $response->clearAllHeaders();
280
+ //@codingStandardsIgnoreStart
281
+ if(function_exists('http_response_code'))
282
+ http_response_code(200);
283
+ //@codingStandardsIgnoreEnd
284
+ $response->setHttpResponseCode(200);
285
+ $response->setRawHeader('HTTP/1.0 200 OK');
286
+ $response->setRawHeader('Status: 200 OK');
287
+ $response->setHeader('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT', true);
288
+ $response->setHeader('Cache-Control', 'no-cache, must-revalidate', true);
289
+ $response->setHeader('Pragma', 'no-cache', true);
290
+ $response->setBody($output);
291
+ }
292
+
293
+ public function indexAction()
294
+ {
295
+ set_time_limit(0);
296
+ ignore_user_abort(false);
297
+
298
+ $request = $this->getRequest();
299
+ $response = $this->getResponse();
300
+ $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
301
+ $contenttype = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : '';
302
+ $server = $request->getServer();
303
+
304
+ if($method == 'POST')
305
+ {
306
+ $helper = Mage::helper('codistosync');
307
+
308
+ if($contenttype == 'text/xml')
309
+ {
310
+ $xml = simplexml_load_string(file_get_contents('php://input'));
311
+
312
+ $ordercontent = $xml->entry->content->children('http://api.codisto.com/schemas/2009/');
313
+
314
+ $storeId = @count($ordercontent->storeid) ? (int)$ordercontent->storeid : 0;
315
+
316
+ if(!$helper->getConfig($storeId))
317
+ {
318
+ $response->clearAllHeaders();
319
+ //@codingStandardsIgnoreStart
320
+ if(function_exists('http_response_code'))
321
+ http_response_code(500);
322
+ //@codingStandardsIgnoreEnd
323
+ $response->setHttpResponseCode(500);
324
+ $response->setRawHeader('HTTP/1.0 500 Security Error');
325
+ $response->setRawHeader('Status: 500 Security Error');
326
+ $response->setHeader('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT', true);
327
+ $response->setHeader('Cache-Control', 'no-cache, must-revalidate', true);
328
+ $response->setHeader('Pragma', 'no-cache', true);
329
+ $response->setBody('Config Error');
330
+ return;
331
+ }
332
+
333
+ if($helper->checkRequestHash(Mage::getStoreConfig('codisto/hostkey', $storeId), $server))
334
+ {
335
+ $productsToReindex = array();
336
+ $ordersProcessed = array();
337
+ $invoicesProcessed = array();
338
+
339
+ $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
340
+
341
+ try
342
+ {
343
+ $connection->addColumn(
344
+ Mage::getConfig()->getTablePrefix() . 'sales_flat_order',
345
+ 'codisto_orderid',
346
+ 'varchar(10)'
347
+ );
348
+ }
349
+ catch(Exception $e)
350
+ {
351
+
352
+ }
353
+
354
+ if($storeId == 0)
355
+ {
356
+ // jump the storeid to first non admin store
357
+ $stores = Mage::getModel('core/store')->getCollection()
358
+ ->addFieldToFilter('is_active', array('neq' => 0))
359
+ ->addFieldToFilter('store_id', array('gt' => 0))
360
+ ->setOrder('store_id', 'ASC');
361
+
362
+ $stores->setPageSize(1)->setCurPage(1);
363
+ $firstStore = $stores->getFirstItem();
364
+ if(is_object($firstStore) && $firstStore->getId())
365
+ $storeId = $firstStore->getId();
366
+ }
367
+
368
+ $store = Mage::app()->getStore($storeId);
369
+
370
+ Mage::app()->setCurrentStore($store);
371
+
372
+ $quote = null;
373
+
374
+ for($Retry = 0; ; $Retry++)
375
+ {
376
+ $productsToReindex = array();
377
+
378
+ try
379
+ {
380
+ $quote = Mage::getModel('sales/quote');
381
+
382
+ $this->ProcessQuote($quote, $xml, $store);
383
+ }
384
+ catch(Exception $e)
385
+ {
386
+ $response = $this->getResponse();
387
+
388
+ $response->clearAllHeaders();
389
+ //@codingStandardsIgnoreStart
390
+ if(function_exists('http_response_code'))
391
+ http_response_code(200);
392
+ //@codingStandardsIgnoreEnd
393
+ $response->setHttpResponseCode(200);
394
+ $response->setRawHeader('HTTP/1.0 200 OK');
395
+ $response->setRawHeader('Status: 200 OK');
396
+ $response->setHeader('Content-Type', 'application/json');
397
+ $response->setHeader('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT', true);
398
+ $response->setHeader('Cache-Control', 'no-cache, must-revalidate', true);
399
+ $response->setHeader('Pragma', 'no-cache', true);
400
+ $response->setBody(Zend_Json::encode(array( 'ack' => 'failed', 'code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString())));
401
+ break;
402
+ }
403
+
404
+ $connection->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
405
+ $connection->beginTransaction();
406
+
407
+ try
408
+ {
409
+ $order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('codisto_orderid', $ordercontent->orderid)->getFirstItem();
410
+
411
+ if($order && $order->getId())
412
+ {
413
+ $this->ProcessOrderCreate($quote, $xml, $productsToReindex, $ordersProcessed, $invoicesProcessed, $store);
414
+
415
+ // $this->ProcessOrderCreate($quote, $order, $xml, $productsToReindex, $ordersProcessed, $invoicesProcessed, $store);
416
+ }
417
+ else
418
+ {
419
+ $this->ProcessOrderCreate($quote, $xml, $productsToReindex, $ordersProcessed, $invoicesProcessed, $store);
420
+ }
421
+
422
+ $connection->commit();
423
+ break;
424
+ }
425
+ catch(Exception $e)
426
+ {
427
+ if($Retry < 5)
428
+ {
429
+ if($e->getCode() == 40001)
430
+ {
431
+ $connection->rollback();
432
+ sleep($Retry * 10);
433
+ continue;
434
+ }
435
+ }
436
+
437
+ $response = $this->getResponse();
438
+
439
+ $response->clearAllHeaders();
440
+ //@codingStandardsIgnoreStart
441
+ if(function_exists('http_response_code'))
442
+ http_response_code(200);
443
+ //@codingStandardsIgnoreEnd
444
+ $response->setHttpResponseCode(200);
445
+ $response->setRawHeader('HTTP/1.0 200 OK');
446
+ $response->setRawHeader('Status: 200 OK');
447
+ $response->setHeader('Content-Type', 'application/json');
448
+ $response->setHeader('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT', true);
449
+ $response->setHeader('Cache-Control', 'no-cache, must-revalidate', true);
450
+ $response->setHeader('Pragma', 'no-cache', true);
451
+ $response->setBody(Zend_Json::encode(array( 'ack' => 'failed', 'code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString())));
452
+
453
+ $connection->rollback();
454
+ break;
455
+ }
456
+ }
457
+
458
+ try
459
+ {
460
+ if(!empty($ordersProcessed))
461
+ {
462
+ Mage::getResourceModel('sales/order')->updateGridRecords($ordersProcessed);
463
+ }
464
+ }
465
+ catch (Exception $e)
466
+ {
467
+
468
+ }
469
+
470
+ try
471
+ {
472
+ if(!empty($invoicesProcessed))
473
+ {
474
+ Mage::getResourceModel('sales/order_invoice')->updateGridRecords($invoicesProcessed);
475
+ }
476
+ }
477
+ catch (Exception $e)
478
+ {
479
+
480
+ }
481
+
482
+ try
483
+ {
484
+
485
+ if(!empty($productsToReindex))
486
+ {
487
+ Mage::getResourceSingleton('cataloginventory/indexer_stock')->reindexProducts($productsToReindex);
488
+ Mage::getResourceSingleton('catalog/product_indexer_price')->reindexProductIds($productsToReindex);
489
+ }
490
+
491
+ }
492
+ catch (Exception $e)
493
+ {
494
+
495
+ }
496
+ }
497
+ else
498
+ {
499
+ $response->clearAllHeaders();
500
+ //@codingStandardsIgnoreStart
501
+ if(function_exists('http_response_code'))
502
+ http_response_code(400);
503
+ //@codingStandardsIgnoreEnd
504
+ $response->setHttpResponseCode(400);
505
+ $response->setRawHeader('HTTP/1.0 400 Security Error');
506
+ $response->setRawHeader('Status: 400 Security Error');
507
+ $response->setHeader('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT', true);
508
+ $response->setHeader('Cache-Control', 'no-cache, must-revalidate', true);
509
+ $response->setHeader('Pragma', 'no-cache', true);
510
+ $response->setBody('Security Error');
511
+ return;
512
+ }
513
+ }
514
+ else
515
+ {
516
+ $response->clearAllHeaders();
517
+ //@codingStandardsIgnoreStart
518
+ if(function_exists('http_response_code'))
519
+ http_response_code(400);
520
+ //@codingStandardsIgnoreEnd
521
+ $response->setHttpResponseCode(400);
522
+ $response->setRawHeader('HTTP/1.0 400 Invalid Content Type');
523
+ $response->setRawHeader('Status: 400 Invalid Content Type');
524
+ $response->setHeader('Expires', 'Thu, 01 Jan 1970 00:00:00 GMT', true);
525
+ $response->setHeader('Cache-Control', 'no-cache, must-revalidate', true);
526
+ $response->setHeader('Pragma', 'no-cache', true);
527
+ $response->setBody('Invalid Content Type');
528
+ return;
529
+ }
530
+ }
531
+ else
532
+ {
533
+ include_once Mage::getBaseDir() . '/errors/404.php';
534
+ }
535
+ }
536
+
537
+ private function ProcessOrderCreate($quote, $xml, &$productsToReindex, &$orderids, &$invoiceids, $store)
538
+ {
539
+ $ordercontent = $xml->entry->content->children('http://api.codisto.com/schemas/2009/');
540
+
541
+ $paypaltransactionid = $ordercontent->orderpayments[0]->orderpayment->transactionid;
542
+ $ordernumberformat = (string)$ordercontent->ordernumberformat;
543
+
544
+ $ordertotal = floatval($ordercontent->ordertotal[0]);
545
+ $ordersubtotal = floatval($ordercontent->ordersubtotal[0]);
546
+ $ordertaxtotal = floatval($ordercontent->ordertaxtotal[0]);
547
+
548
+ $ordersubtotal = $store->roundPrice($ordersubtotal);
549
+ $ordersubtotalincltax = $store->roundPrice($ordersubtotal + $ordertaxtotal);
550
+ $ordertotal = $store->roundPrice($ordertotal);
551
+
552
+ $ebaysalesrecordnumber = (string)$ordercontent->ebaysalesrecordnumber;
553
+ if(!$ebaysalesrecordnumber)
554
+ $ebaysalesrecordnumber = '';
555
+
556
+ $ebayusername = (string)$ordercontent->ebayusername;
557
+ if(!$ebayusername)
558
+ $ebayusername = '';
559
+
560
+ $quoteConverter = Mage::getModel('sales/convert_quote');
561
+
562
+ $quote->reserveOrderId();
563
+ $order = $quoteConverter->addressToOrder($quote->getShippingAddress());
564
+ $order->setBillingAddress($quoteConverter->addressToOrderAddress($quote->getBillingAddress()));
565
+ $order->setShippingAddress($quoteConverter->addressToOrderAddress($quote->getShippingAddress()));
566
+ $order->setPayment($quoteConverter->paymentToOrderPayment($quote->getPayment()));
567
+ $order->setCustomer($quote->getCustomer());
568
+ $order->setCodistoOrderid($ordercontent->orderid);
569
+
570
+ if(preg_match('/\{ordernumber\}/', $ordernumberformat))
571
+ {
572
+ $incrementId = preg_replace('/\{ordernumber\}/', (string)$order->getIncrementId(), $ordernumberformat);
573
+ $order->setIncrementId($incrementId);
574
+ }
575
+ else
576
+ {
577
+ $incrementId = $ordernumberformat.''.(string)$order->getIncrementId();
578
+ $order->setIncrementId($incrementId);
579
+ }
580
+
581
+ $weight_total = 0;
582
+
583
+ $quoteItems = $quote->getItemsCollection()->getItems();
584
+ $quoteIdx = 0;
585
+
586
+ foreach($ordercontent->orderlines->orderline as $orderline)
587
+ {
588
+ if($orderline->productcode[0] != 'FREIGHT')
589
+ {
590
+ $adjustStock = true;
591
+
592
+ $product = null;
593
+
594
+ $productcode = $orderline->productcode[0];
595
+ if($productcode == null)
596
+ $productcode = '';
597
+ else
598
+ $productcode = (string)$productcode;
599
+
600
+ $productname = $orderline->productname[0];
601
+ if($productname == null)
602
+ $productname = '';
603
+ else
604
+ $productname = (string)$productname;
605
+
606
+ $productid = $orderline->externalreference[0];
607
+ if($productid != null)
608
+ {
609
+ $productid = intval($productid);
610
+
611
+ $product = Mage::getModel('catalog/product')->load($productid);
612
+ if($product->getId())
613
+ {
614
+ $productcode = $product->getSku();
615
+ $productname = $product->getName();
616
+ }
617
+ else
618
+ {
619
+ $product = null;
620
+ }
621
+ }
622
+
623
+ if(!$product)
624
+ {
625
+ $product = Mage::getModel('catalog/product');
626
+ $adjustStock = false;
627
+ }
628
+
629
+ $qty = (int)$orderline->quantity[0];
630
+ $subtotalinctax = floatval($orderline->linetotalinctax[0]);
631
+ $subtotal = floatval($orderline->linetotal[0]);
632
+
633
+ $price = floatval($orderline->price[0]);
634
+ $priceinctax = floatval($orderline->priceinctax[0]);
635
+ $taxamount = $priceinctax - $price;
636
+ $taxpercent = $price == 0 ? 0 : round($priceinctax / $price - 1.0, 2) * 100;
637
+ $weight = floatval($orderline->weight[0]);
638
+ if($weight == 0)
639
+ $weight = 1;
640
+
641
+ $weight_total += $weight;
642
+
643
+ $orderItem = $quoteConverter->itemToOrderItem($quoteItems[$quoteIdx]);
644