~openerp-community/openobjectworks/magentoconnector

« back to all changes in this revision

Viewing changes to OpenERPSync/openerp-synchro.php

  • Committer: Sharoon Thomas
  • Date: 2009-06-02 16:30:49 UTC
  • Revision ID: sharoonthomas@teagarden.in-20090602163049-zsbek6lukohwbmgq
Added stores, websites and groups enumeration

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Magento OpenERP Smile synchro
 
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   Mage
 
16
 * @package    Mage_OpenERP
 
17
 * @copyright  Copyright (c) 2008 Smile S.A. (http://www.smile.fr)
 
18
 * @authors     Sylvain Pamart, Rapha�l Valyi
 
19
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 
20
 */
 
21
 
 
22
        /** Wrapper arround our remote functions */ 
 
23
        class OpenERPConnector {
 
24
                
 
25
                /** Update the Magento sale order with the update date OpenERP sale order
 
26
                * @param array sale_order Array: OpenERP sale order XML/RPC hash
 
27
                * @return int id: updated Magento sale order id
 
28
                */
 
29
                function update_sale_order($sale_order){
 
30
                        $sale_order = $sale_order[0];
 
31
                        $order = Mage::getModel('sales/order')->load($sale_order['magento_id']);
 
32
                        
 
33
                        //      status matching TODO can't do better?
 
34
                        if($sale_order['status'] == ''){$order->setStatus(Mage_Sales_Model_Order::STATE_NEW, true);}
 
35
                        if($sale_order['status'] == 'progress'){$order->setStatus(Mage_Sales_Model_Order::STATE_PROCESSING, true);}
 
36
                        if($sale_order['status'] == 'shipping_except'){$order->setStatus(Mage_Sales_Model_Order::STATE_COMPLETE, true);}
 
37
                        if($sale_order['status'] == 'invoice_except'){$order->setStatus(Mage_Sales_Model_Order::STATE_COMPLETE, true);}
 
38
                        if($sale_order['status'] == 'done'){$order->setStatus(Mage_Sales_Model_Order::STATE_CLOSED, true);}
 
39
                        if($sale_order['status'] == 'cancel'){$order->setStatus(Mage_Sales_Model_Order::STATE_CANCELED, true);}
 
40
                        if($sale_order['status'] == 'waiting_date'){$order->setStatus(Mage_Sales_Model_Order::STATE_HOLDED, true);}
 
41
                        
 
42
                        $order->save();
 
43
                        return $order->getId();
 
44
                }
 
45
                
 
46
                
 
47
                /** Grab the Magento sale order with the requested id
 
48
                * @param int sale_order_id int: Magento sale order id
 
49
                * @return array XML/RPC hash of the Magento sale order
 
50
                */
 
51
                function get_sale_order($sale_order_id) {               
 
52
                        $productArray=array();  // sale order line product wrapper
 
53
                        
 
54
                        // Magento required models
 
55
                        $order = Mage::getModel('sales/order')->load($sale_order_id);
 
56
                        $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
 
57
                        
 
58
                        //      walk the sale order lines
 
59
                        foreach ($order->getAllItems() as $item) {
 
60
                                $productArray[] = array(
 
61
                                        "product_sku" => $item->getSku(),
 
62
                                        "product_magento_id" => $item->getProductId(),
 
63
                                        "product_name" => $item->getName(),
 
64
                                        "product_qty" => $item->getQtyOrdered(),
 
65
                                        "product_price" => $item->getPrice(),
 
66
                                        "product_discount_amount" => $item->getDiscountAmount(),
 
67
                                        "product_row_price" => $item->getPrice() - $item->getDiscountAmount(),
 
68
                                );
 
69
                        }
 
70
                        
 
71
                        $streetBA=$order->getBillingAddress()->getStreet();
 
72
                        $streetSA=$order->getShippingAddress()->getStreet();
 
73
 
 
74
                        $saleorder =  array(
 
75
                                "id" => $order->getId(),
 
76
                                "store_id" => $order->getStoreId(),
 
77
                                "payment"=> $order->getPayment()->getMethod(),
 
78
                                "shipping_amount" => $order->getShippingAmount(),
 
79
                                "grand_total" => $order->getGrandTotal(),
 
80
                                "date"=> $order->getCreatedAt(),
 
81
                                "lines" => $productArray,
 
82
                                "shipping_address" =>    array(
 
83
                                        "firstname"=> $order->getShippingAddress()->getFirstname(),
 
84
                                        "lastname"=> $order->getShippingAddress()->getLastname(),
 
85
                                        "company"=> $order->getShippingAddress()->getCompany(),
 
86
                                        "street" => $streetSA[0],
 
87
                                        "street2" => (count($streetSA)==2)?$streetSA[1]:'',
 
88
                                        "city"=> $order->getShippingAddress()->getCity(),
 
89
                                        "postcode"=> $order->getShippingAddress()->getPostcode(),
 
90
                                        "country"=> $order->getShippingAddress()->getCountry(),
 
91
                                        "phone"=> $order->getShippingAddress()->getTelephone()
 
92
                                ),
 
93
                                "billing_address" =>     array(
 
94
                                        "firstname"=> $order->getBillingAddress()->getFirstname(),
 
95
                                        "lastname"=> $order->getBillingAddress()->getLastname(),
 
96
                                        "company"=> $order->getBillingAddress()->getCompany(),
 
97
                                        "street" => $streetBA[0],
 
98
                                        "street2" => (count($streetBA)==2)?$streetBA[1]:'',
 
99
                                        "city"=> $order->getBillingAddress()->getCity(),
 
100
                                        "postcode"=> $order->getBillingAddress()->getPostcode(),
 
101
                                        "country"=> $order->getBillingAddress()->getCountry(),
 
102
                                        "phone"=> $order->getBillingAddress()->getTelephone()
 
103
                                ),
 
104
                                "customer" => array(
 
105
                                        "customer_id"=> $customer->getId(),
 
106
                                        "customer_name"=> $customer->getName(),
 
107
                                        "customer_email"=> $customer->getEmail()
 
108
                                ),
 
109
                                
 
110
                        );
 
111
                        
 
112
                        return $saleorder;
 
113
                }
 
114
                
 
115
                
 
116
                /**
 
117
                * @param int last_sale_order_id
 
118
                * @return array saleorders
 
119
                */
 
120
                function sale_orders_sync($last_sale_order_id) {
 
121
                        $saleorders=array();  
 
122
 
 
123
                        //      retrieve the next sale orders
 
124
                        $order_collection = Mage::getResourceModel('sales/order_collection')
 
125
                                ->addAttributeToSelect('entity_id')
 
126
                                ->addAttributeToSort('entity_id', 'desc')
 
127
                                ->load();
 
128
                                
 
129
                        $max_order_id=$order_collection->getFirstItem()->getId();
 
130
                        
 
131
                        /* iterate over the sale orders */
 
132
                        for ($id = $last_sale_order_id+1; $id <= $max_order_id; $id++) {
 
133
                                $productArray=array();  //      sale order line product wrapper
 
134
                                
 
135
                                //      Magento required models
 
136
                                $order = Mage::getModel('sales/order')->load($id);
 
137
                                $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
 
138
                                
 
139
                                //      Walk the sale order lines
 
140
                                foreach ($order->getAllItems() as $item) {
 
141
                                        $productArray[] = array(
 
142
                                                "product_sku" => $item->getSku(),
 
143
                                                "product_magento_id" => $item->getProductId(),
 
144
                                                "product_name" => $item->getName(),
 
145
                                                "product_qty" => $item->getQtyOrdered(),
 
146
                                                "product_price" => $item->getPrice(),
 
147
                                                "product_discount_amount" => $item->getDiscountAmount(),
 
148
                                                "product_row_price" => $item->getPrice() - $item->getDiscountAmount()
 
149
                                        );
 
150
                                }
 
151
                                
 
152
                        
 
153
                                $streetBA=$order->getBillingAddress()->getStreet();
 
154
                                $streetSA=$order->getShippingAddress()->getStreet();
 
155
                                
 
156
                                $saleorders[] =  array(
 
157
                                        "id" => $order->getId(),
 
158
                                        "store_id" => $order->getStoreId(),
 
159
                                        "payment"=> $order->getPayment()->getMethod(),
 
160
                                        "shipping_amount" => $order->getShippingAmount(),
 
161
                                        "grand_total" => $order->getGrandTotal(),
 
162
                                        "date"=> $order->getCreatedAt(),
 
163
                                        "lines" => $productArray,
 
164
                                        "shipping_address" =>    array(
 
165
                                                "firstname"=> $order->getShippingAddress()->getFirstname(),
 
166
                                                "lastname"=> $order->getShippingAddress()->getLastname(),
 
167
                                                "company"=> $order->getShippingAddress()->getCompany(),
 
168
                                                "street"=> $streetSA[0],
 
169
                                                "street2"=> (count($streetSA)==2)?$streetSA[1]:'',
 
170
                                                "city"=> $order->getShippingAddress()->getCity(),
 
171
                                                "postcode"=> $order->getShippingAddress()->getPostcode(),
 
172
                                                "country"=> $order->getShippingAddress()->getCountry(),
 
173
                                                "phone"=> $order->getShippingAddress()->getTelephone()
 
174
                                        ),
 
175
                                        "billing_address" =>     array(
 
176
                                                "firstname"=> $order->getBillingAddress()->getFirstname(),
 
177
                                                "lastname"=> $order->getBillingAddress()->getLastname(),
 
178
                                                "company"=> $order->getBillingAddress()->getCompany(),
 
179
                                                "street" => $streetBA[0],
 
180
                                                "street2" => (count($streetBA)==2)?$streetBA[1]:'',
 
181
                                                "city"=> $order->getBillingAddress()->getCity(),
 
182
                                                "postcode"=> $order->getBillingAddress()->getPostcode(),
 
183
                                                "country"=> $order->getBillingAddress()->getCountry(),
 
184
                                                "phone"=> $order->getBillingAddress()->getTelephone()
 
185
                                        ),
 
186
                                        "customer" => array(
 
187
                                                "customer_id"=> $customer->getId(),
 
188
                                                "customer_name"=> $customer->getName(),
 
189
                                                "customer_email"=> $customer->getEmail()
 
190
                                        ),
 
191
                                );
 
192
                        }
 
193
                        
 
194
                        return $saleorders;
 
195
                }
 
196
                                        
 
197
        }
 
198
        
 
199
 
 
200
        /** Debug utility function ; use it to log data if you want to investigate on what is going wrong */
 
201
        function debug($s) {
 
202
                $fp = fopen("./debug.xmlrpc.log","a+");
 
203
                fwrite($fp, $s."\n");
 
204
                fclose($fp);
 
205
        }
 
206
        
 
207
        
 
208
        /** Debug utility function ; use it to log an arraysif you want to investigate on what is going wrong */
 
209
        function debug_arr($para_arr,$tab=''){
 
210
                if (is_array($para_arr))
 
211
                {
 
212
                        foreach($para_arr as $key=>$values)
 
213
                        {
 
214
                                debug($tab.'Key :'.$key.' Value :'.$values);
 
215
                        }
 
216
                }
 
217
        }
 
218
 
 
219
 
 
220
//script when page loads, either from XML/RPC, either browser HTTP (to check the installation)
 
221
 
 
222
if (!function_exists('getallheaders')) {//dirty fix (but not critical) when running in CGI mode; see: http://code.google.com/p/magento-openerp-smile-synchro/issues/detail?id=12
 
223
        function getallheaders() {
 
224
           foreach($_SERVER as $name => $value)
 
225
               if(substr($name, 0, 5) == 'HTTP_')
 
226
                   $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', 
 
227
        substr($name, 5)))))] = $value;
 
228
           return $headers;
 
229
        }
 
230
}
 
231
 
 
232
$headers = getallheaders();
 
233
//echo stripos($headers['Accept'], 'text');
 
234
if (isset($headers['Accept'])) {
 
235
        $request_type = stripos($headers['Accept'], 'text');
 
236
        if ($request_type >=0) {
 
237
                browserTestMessage() ;
 
238
        } else {
 
239
                initRPCServer();
 
240
        }
 
241
} else {
 
242
        initRPCServer();
 
243
}
 
244
 
 
245
 
 
246
/** Init the XML/RPC server */
 
247
function initRPCServer() {
 
248
        require_once('../../../../Mage.php');   //Magento import
 
249
        Mage::app();    //bootstarp the Magento Model layer (at least) and autoload function
 
250
        
 
251
        require_once('../../../../../lib/Zend/XmlRpc/Server.php');
 
252
        $server = new Zend_XmlRpc_Server();
 
253
        $server->setClass('OpenERPConnector');
 
254
        echo $server->handle();
 
255
}
 
256
 
 
257
 
 
258
/** Test utility when calling the page from your HTTP browser */
 
259
function browserTestMessage() {
 
260
                echo("<h1>Hello, This is the Magento/OpenERP Connector by <a href='http://www.smile.fr'>Smile.fr</a></h1> We will now perform some basic test about your installation.<br/> You should get and no error or warning here</br></br></br>");
 
261
                echo("1) Presence of connector file ---> YES<br/><br/>");
 
262
                echo("2) Bootsrapping Magento... <br/>");
 
263
                echo("- IF YOU GET AN ERROR HERE LIKE 'Uncaught exception 'Exception' with message 'Warning: Cannot modify header information' THEN CLEAR YOUR BROWSER COOKIES AND REFRESH THE PAGE TO TEST AGAIN -<br/>");
 
264
                require_once('../../../Mage.php');      //Magento import
 
265
                Mage::app();
 
266
                echo("<br/>&nbsp; &nbsp; &nbsp; &nbsp; --->OK<br/><br/>");
 
267
                echo("3) minimal (but might not be enough) testing of your PHP XML/RPC lib ");
 
268
                require_once('../../../../lib/Zend/XmlRpc/Server.php');
 
269
                $server = new Zend_XmlRpc_Server();
 
270
                $server->setClass('OpenERPConnector');
 
271
                echo("--->OK<br/>");
 
272
                echo("4) SECURITY: DON'T FORGET TO SET UP YOUR APACHE PROPERLY (NOT TESTED HERE) TO AVOID CONNECTION TO THAT PAGE FROM ANY OTHER IP THAN YOU OPENERP!!!<br/><br/>");
 
273
                echo("5) Information about your PHP installation:<br/>");
 
274
                phpinfo();
 
275
}
 
276
?>
 
 
b'\\ No newline at end of file'