Fulfillment_Ordersync - Version 1.0.0

Version Notes

To sync the Magento orders with Storeship for shipping

Download this release

Release Info

Developer Storeship
Extension Fulfillment_Ordersync
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

app/code/community/Fulfillment/Ordersync/Helper/Data.php ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Fulfillment_Ordersync_Helper_Data extends Mage_Core_Helper_Abstract{
4
+
5
+
6
+ }
app/code/community/Fulfillment/Ordersync/controllers/IndexController.php ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Fulfillment_Ordersync_IndexController extends Mage_Core_Controller_Front_Action{
4
+ /**
5
+ * default action
6
+ * @access public
7
+ * @return void
8
+ * @author Pankaj Pareek
9
+ */
10
+ public function indexAction(){
11
+
12
+ $status = 1; // 1 = Enabled, 0 = Disabled
13
+
14
+ $apiUsername = Mage::getStoreConfig('ordersync/display_settings/username');
15
+ $apiPassword = Mage::getStoreConfig('ordersync/display_settings/password');
16
+ $this->loadLayout();
17
+ $this->renderLayout();
18
+
19
+ $parms = $this->getRequest()->getParams();
20
+ $ssfUsername = $parms['ssfUsername'];
21
+ $ssfPassword = $parms['ssfPassword'];
22
+ $ssfAPICall = $parms['ssfAPICall'];
23
+
24
+
25
+ if($status == 1 && !empty($ssfUsername) && !empty($ssfPassword) && $ssfUsername == $apiUsername && $ssfPassword == $apiPassword){
26
+ // Download list of orders
27
+ if(isset($ssfAPICall) && $ssfAPICall == "importCSV"){
28
+
29
+ $collection = Mage::getResourceModel('sales/order_collection')->addAttributeToSelect('*');
30
+ $collection->addFieldToFilter('status', 'processing');
31
+
32
+ $csvstring = "";
33
+
34
+ foreach ($collection as $col) {
35
+
36
+ $_shippingAddress = $col->getShippingAddress();
37
+ $id = $col->getId();
38
+
39
+ $basetotal = $col->getBaseSubtotal();
40
+ $baseshippingamount = $col->getBaseShippingAmount();
41
+ $basegrandtotal = $col->getBaseGrandTotal();
42
+ $baseshippingaaxamount = $col->getBaseShippingTaxAmount();
43
+ //echo $customerid = $col->getCustomerId();
44
+
45
+ $order_id = $col->getIncrementId();
46
+ $createdate = $col->getCreatedAt();
47
+ $created_date = strtotime($createdate);
48
+ $updateddate = $col->getUpdatedAt();
49
+ $updated_date = strtotime($updateddate);
50
+ $get_state = $col->getState();
51
+ $get_status = $col->getStatus();
52
+ $customer_name = $_shippingAddress->getFirstname()." ".$_shippingAddress->getLastname();
53
+ $address1 = $_shippingAddress->getStreet1();
54
+ $address2 = $_shippingAddress->getStreet2();
55
+ $city = $_shippingAddress->getCity();
56
+ $state = $_shippingAddress->getRegion();
57
+ $country_code = $_shippingAddress->getCountry_id();
58
+ $country = Mage::app()->getLocale()->getCountryTranslation($country_code);
59
+ $customer_email = $_shippingAddress->getEmail();
60
+ $customerNote = $col->getCustomerNote();
61
+ $telephone = $_shippingAddress->getTelephone();
62
+
63
+
64
+ $products = array();
65
+ $items = $col->getAllVisibleItems();
66
+ foreach($items as $i) {
67
+ $product_it['pid'] = $i->getProductId();
68
+ $product_it['sku'] = $i->getSku();
69
+ $product_it['qty'] = $i->getQtyOrdered();
70
+ $product_it['price'] = $i->getPrice();
71
+ $product_it['name'] = $i->getName();
72
+ $product_it['order_id'] = $order_id;
73
+
74
+ $products[] = $product_it;
75
+ }
76
+
77
+ $str = serialize($products);
78
+ $products_str = urlencode($str);
79
+
80
+ $csvv = "order_id=".$this->_csvSafe($order_id).",".
81
+ "get_status=".$this->_csvSafe($get_status).",".
82
+ "created_date=".$this->_csvSafe($created_date).",".
83
+ "updated_date=".$this->_csvSafe($updated_date).",".
84
+ "get_state=".$this->_csvSafe($get_state).",".
85
+ "address1=".$this->_csvSafe($address1).",".
86
+ "address2=".$this->_csvSafe($address2).",".
87
+ "city=".$this->_csvSafe($city).",".
88
+ "state=".$this->_csvSafe($state).",".
89
+ "country=".$this->_csvSafe($country).",".
90
+ "customer_email=".$this->_csvSafe($customer_email).",".
91
+ "customer_name=".$this->_csvSafe($customer_name).",".
92
+ "basetotal=".$this->_csvSafe($basetotal).",".
93
+ "baseshippingamount=".$this->_csvSafe($baseshippingamount).",".
94
+ "basegrandtotal=".$this->_csvSafe($basegrandtotal).",".
95
+ "baseshippingaaxamount=".$this->_csvSafe($baseshippingaaxamount).",".
96
+ "customerNote=".$this->_csvSafe($customerNote).",".
97
+ "telephone=".$this->_csvSafe($telephone).",".
98
+ "products=".$products_str;
99
+
100
+ if(isset($csvstring) && $csvstring!="")
101
+ {
102
+ $csvstring = $csvstring."\r\n".$csvv;
103
+
104
+ } else {
105
+ $csvstring = $csvv;
106
+ }
107
+ }
108
+
109
+ echo $csvstring;
110
+ }
111
+
112
+
113
+ //----- Generate shipment and updae order -----------------------
114
+ if(isset($ssfAPICall) && $ssfAPICall == "importShipped"){
115
+
116
+ $ssfData = $parms['ssfData'];
117
+ $ssfStock = $parms['ssfStock'];
118
+ $orders = unserialize(base64_decode($ssfData));
119
+ $prds_stocks = unserialize(base64_decode($ssfStock));
120
+
121
+
122
+ if(count($orders))
123
+ {
124
+ foreach($orders as $oval)
125
+ {
126
+ $orderno = $oval['orderId'];
127
+ $tracking = $oval['trackingNumber'];
128
+ $shipDate = $oval['shipDate'];
129
+
130
+ $this->_processOrder($orderno, $tracking);
131
+
132
+ }
133
+ echo(1);
134
+ } else {
135
+ echo(0);
136
+ }
137
+
138
+
139
+ if(count($prds_stocks))
140
+ {
141
+ foreach($prds_stocks as $pval)
142
+ {
143
+ $sku = $pval['sku'];
144
+ $stock = $pval['stock'];
145
+ $name = $pval['name'];
146
+
147
+ $this->_productStockUpdate($sku, $stock);
148
+
149
+ }
150
+ echo(1);
151
+ } else {
152
+ echo(0);
153
+ }
154
+
155
+
156
+ }
157
+
158
+
159
+ //---------Get Prodcuts to import in to sytem -------------------------
160
+
161
+ if(isset($ssfAPICall) && $ssfAPICall == "productList"){
162
+
163
+ $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
164
+
165
+ $productlist = array();
166
+ foreach($collection as $product) {
167
+
168
+ $productItem['pid'] = $product->getId();
169
+ $productItem['name'] = $product->getName();
170
+ $productItem['price'] = $product->getPrice();
171
+ $productItem['sku'] = $product->getSku();
172
+ $productItem['weight'] = $product->getWeight();
173
+ $productItem['qty'] = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->getQty();
174
+ $productlist[] = $productItem;
175
+
176
+ }
177
+
178
+ $str = serialize($productlist);
179
+ echo $strenc = urlencode($str);
180
+
181
+ }
182
+ //----------------------------------
183
+
184
+
185
+ } else {
186
+ echo("-1");
187
+ exit;
188
+ }
189
+
190
+ echo("-1");
191
+ exit;
192
+ }
193
+
194
+
195
+ function _csvSafe($string){
196
+ return str_replace('"', '\"', $string);
197
+ }
198
+
199
+
200
+ //---------function to update the product stock --------
201
+
202
+ function _productStockUpdate($sku, $qty){
203
+
204
+ $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
205
+ $connectionW = Mage::getSingleton('core/resource')->getConnection('core_write');
206
+
207
+ $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
208
+ if($product)
209
+ {
210
+ $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
211
+ $stockItemData = $stockItem->getData();
212
+ if (empty($stockItemData)) {
213
+
214
+ // Create the initial stock item object
215
+ $stockItem->setData('manage_stock',1);
216
+ $stockItem->setData('is_in_stock',$qty ? 1 : 0);
217
+ $stockItem->setData('use_config_manage_stock', 0);
218
+ $stockItem->setData('stock_id',1);
219
+ $stockItem->setData('product_id',$product->getId());
220
+ $stockItem->setData('qty',0);
221
+ $stockItem->save();
222
+
223
+ // Init the object again after it has been saved so we get the full object
224
+ $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
225
+ }
226
+
227
+ // Set the quantity
228
+ $stockItem->setData('is_in_stock',$qty ? 1 : 0);
229
+ $stockItem->setData('qty',$qty);
230
+ $stockItem->save();
231
+ $product->save();
232
+
233
+ }
234
+
235
+ }
236
+
237
+ //---------function to generate shipment and add tracking number --------
238
+
239
+ function _processOrder($ORDERIDPP, $order_trackingno) {
240
+
241
+ $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
242
+ $connectionW = Mage::getSingleton('core/resource')->getConnection('core_write');
243
+
244
+
245
+ $orders = array();
246
+
247
+ $odrid = $ORDERIDPP;
248
+ $orders[$ORDERIDPP] = array($order_trackingno, 'Custom');
249
+
250
+
251
+
252
+ foreach($orders as $orderno => $tinfo) {
253
+
254
+ $order = Mage::getModel('sales/order'); //Load the orders Model, so we can fetch and verify the order number
255
+
256
+ if($order->loadByIncrementId($orderno)){
257
+
258
+ $convertor = Mage::getModel('sales/convert_order'); //Load the convert_order model, this allows us to 'convert' the order to a shipment (it can also do credit memos.)
259
+ $shipment = $convertor->toShipment($order); //Tells the convertor we want to convert it to a shipment
260
+
261
+ if($shipment) { //This foreach simply gets all the items, and adds them to our shipment.
262
+
263
+ foreach ($order->getAllItems() as $orderItem) {
264
+ if (!$orderItem->getQtyToShip()) {
265
+ continue;
266
+ }
267
+ if ($orderItem->getIsVirtual()) {
268
+ continue;
269
+ }
270
+ $item = $convertor->itemToShipmentItem($orderItem); //Prepares to add the item
271
+ $qty = $orderItem->getQtyToShip();
272
+ $item->setQty($qty); //Sets the quantity of that item to be shipped. Line above we set the quantity we wanted to ship
273
+ $shipment->addItem($item); //Adds the item
274
+ }
275
+
276
+ $data = array();
277
+ $data['carrier_code'] = 'custom'; //This needs to be custom, it allows us to enter a courier's company name in the 'title' section.
278
+ $data['title'] = $tinfo[1]; //Couriers company name
279
+ $data['number'] = $order_trackingno;//$tinfo[0]; //The tracking number
280
+
281
+ $track = Mage::getModel('sales/order_shipment_track')->addData($data);
282
+
283
+ $shipment->addTrack($track); //Adds the tracking to the shipment
284
+ $shipment->register();
285
+ $shipment->addComment(null, true); //Adds any comments if there are any. Includes the comment in the email if required.
286
+ $shipment->setEmailSent(true); //This marks the shipment as customer notified, otherwise it doesn't.
287
+ $shipment->getOrder()->setIsInProcess(true);
288
+
289
+ try {
290
+ //Now we try to save the 'transaction', just to confirm everything has worked without the hitch.
291
+ $transactionSave = Mage::getModel('core/resource_transaction')
292
+ ->addObject($shipment)
293
+ ->addObject($shipment->getOrder())
294
+ ->save();
295
+ } catch (Exception $e) {
296
+ //print_r($e); //Prints out any exceptions that have been thrown up, hopefully none!
297
+ continue;
298
+ }
299
+
300
+ $shipment->sendEmail(true, ''); //Finally, Send email customer
301
+
302
+ }
303
+
304
+ }
305
+
306
+ }
307
+ }
308
+
309
+
310
+
311
+
312
+ }
app/code/community/Fulfillment/Ordersync/etc/adminhtml.xml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <acl>
4
+ <resources>
5
+ <all>
6
+ <title>Allow Everything</title>
7
+ </all>
8
+ <admin>
9
+ <children>
10
+ <system>
11
+ <children>
12
+ <config>
13
+ <children>
14
+ <ordersync translate="title">
15
+ <title>Ordersync Permission Setting</title>
16
+ <sort_order>50</sort_order>
17
+ </ordersync>
18
+ </children>
19
+ </config>
20
+ </children>
21
+ </system>
22
+ </children>
23
+ </admin>
24
+ </resources>
25
+ </acl>
26
+ </config>
app/code/community/Fulfillment/Ordersync/etc/config.xml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+
3
+ <config>
4
+ <modules>
5
+ <Fulfillment_Ordersync>
6
+ <version>1.0.0</version>
7
+ </Fulfillment_Ordersync>
8
+ </modules>
9
+ <admin>
10
+ <routers>
11
+ <fulfillment>
12
+ <use>admin</use>
13
+ <args>
14
+ <module>Fulfillment_Ordersync</module>
15
+ <frontName>fulfillment</frontName>
16
+ </args>
17
+ </fulfillment>
18
+ </routers>
19
+ </admin>
20
+
21
+ <global>
22
+ <helpers>
23
+ <ordersync>
24
+ <class>Fulfillment_Ordersync_Helper</class>
25
+ </ordersync>
26
+ </helpers>
27
+ </global>
28
+ </config>
app/code/community/Fulfillment/Ordersync/etc/system.xml ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <tabs>
4
+ <fulfillment translate="label">
5
+ <label>Fulfillment Add-ons</label>
6
+ <sort_order>400</sort_order>
7
+ </fulfillment>
8
+ </tabs>
9
+ <sections>
10
+ <ordersync translate="label" module="ordersync">
11
+ <label>Order Sync</label>
12
+ <tab>fulfillment</tab>
13
+ <frontend_type>text</frontend_type>
14
+ <sort_order>299</sort_order>
15
+ <show_in_default>1</show_in_default>
16
+ <show_in_website>1</show_in_website>
17
+ <show_in_store>1</show_in_store>
18
+ <groups>
19
+ <display_settings translate="label">
20
+ <label>General Setting</label>
21
+ <frontend_type>text</frontend_type>
22
+ <sort_order>1</sort_order>
23
+ <show_in_default>1</show_in_default>
24
+ <show_in_website>1</show_in_website>
25
+ <show_in_store>1</show_in_store>
26
+ <fields>
27
+ <enabled translate="label comment">
28
+ <label>Enabled</label>
29
+ <frontend_type>select</frontend_type>
30
+ <source_model>adminhtml/system_config_source_yesno</source_model>
31
+ <sort_order>1</sort_order>
32
+ <show_in_default>1</show_in_default>
33
+ <show_in_website>1</show_in_website>
34
+ <show_in_store>1</show_in_store>
35
+ </enabled>
36
+ <username translate="label comment">
37
+ <label>Username</label>
38
+ <frontend_type>text</frontend_type>
39
+ <sort_order>2</sort_order>
40
+ <show_in_default>1</show_in_default>
41
+ <show_in_website>1</show_in_website>
42
+ <show_in_store>1</show_in_store>
43
+ <comment>Username of API</comment>
44
+ </username>
45
+ <password translate="label comment">
46
+ <label>Password</label>
47
+ <frontend_type>text</frontend_type>
48
+ <sort_order>3</sort_order>
49
+ <show_in_default>1</show_in_default>
50
+ <show_in_website>1</show_in_website>
51
+ <show_in_store>1</show_in_store>
52
+ <comment>Password of API</comment>
53
+ </password>
54
+ </fields>
55
+ </display_settings>
56
+ </groups>
57
+ </ordersync>
58
+ </sections>
59
+ </config>
app/etc/modules/Fulfillment_Ordersync.xml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * Fulfillment
5
+ *
6
+ * Fulfillment Ordersync extension
7
+ * @category Fulfillment
8
+ * @package Fulfillment_Ordersync
9
+ * @copyright Copyright (c) 2011 Fulfillment
10
+ * @author Fulfillment Dev Team
11
+
12
+ */
13
+
14
+ -->
15
+ <config>
16
+ <modules>
17
+ <Fulfillment_Ordersync>
18
+ <active>true</active>
19
+ <codePool>community</codePool>
20
+ </Fulfillment_Ordersync>
21
+ </modules>
22
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Fulfillment_Ordersync</name>
4
+ <version>1.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/osl-3.0.php">OSL v3.0</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>To sync the Magento orders with Storeship for shipping</summary>
10
+ <description>To sync the Magento orders with Storeship for shipping</description>
11
+ <notes>To sync the Magento orders with Storeship for shipping</notes>
12
+ <authors><author><name>Storeship</name><user>Storeship</user><email>sales@storeship.co.uk</email></author></authors>
13
+ <date>2014-05-09</date>
14
+ <time>06:52:09</time>
15
+ <contents><target name="magecommunity"><dir name="Fulfillment"><dir name="Ordersync"><dir name="Helper"><file name="Data.php" hash="018943e4fed216c0566687443a3c3513"/></dir><dir name="controllers"><file name="IndexController.php" hash="6c50864d7440d8b6363290d95021ca40"/></dir><dir name="etc"><file name="adminhtml.xml" hash="760529eb184e19c83cb61afc82abb789"/><file name="config.xml" hash="d971cff9c0809c03148410d704ffdd0e"/><file name="system.xml" hash="d3ce08ae5da3d7cbe7694da642ca489f"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Fulfillment_Ordersync.xml" hash="8af85a52db86eb7e0e0f86d0d741d0fc"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>