~openerp-community/openobjectworks/magentoconnector

« back to all changes in this revision

Viewing changes to OpenERPSync/Model/Observer.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
# Needed to fix a bug in ZF 1.5.1
 
4
# http://framework.zend.com/issues/browse/ZF-2978
 
5
class My_XmlRpc_Client extends Zend_XmlRpc_Client
 
6
{
 
7
    public function call($method, $params = array())
 
8
    {
 
9
        $request = new Zend_XmlRpc_Request($method, $params);
 
10
 
 
11
        $this->doRequest($request);
 
12
 
 
13
        if ( $this->getLastResponse()->isFault() ) 
 
14
                {
 
15
            $fault = $this->getLastResponse()->getFault();
 
16
            throw new Zend_XmlRpc_Client_FaultException($fault->getMessage(),
 
17
                                                        $fault->getCode());
 
18
        }
 
19
 
 
20
        return $this->getLastResponse()->getReturnValue();
 
21
    }
 
22
}
 
23
 
 
24
class Smile_OpenERPSync_Model_Observer
 
25
    extends Mage_Core_Model_Abstract 
 
26
{
 
27
        /**
 
28
        * Export an order on completion
 
29
        *
 
30
        * @triggeredby: checkout_type_onepage_save_order_after 
 
31
        *               (when sales_order_place_after fires the order doesn't have an id yet)
 
32
        * @param $eventArgs array "order"=>$order
 
33
        */
 
34
        public function exportOnOrderEvent($observer) 
 
35
        {
 
36
                if (Mage::getStoreConfig('openerpsync/settings/enabled'))
 
37
                {
 
38
                        $order = $observer->getEvent()->getOrder();
 
39
                        $saleOrder = $this->get_sale_order($order);
 
40
                        $this->createOrder($saleOrder);
 
41
                }
 
42
                return $this;
 
43
        }
 
44
 
 
45
        
 
46
        function debugEnabled()
 
47
        {
 
48
                return Mage::getStoreConfig('openerpsync/settings/debug_flag');
 
49
        }
 
50
        
 
51
        function getServerURL()
 
52
        {
 
53
                $server =   Mage::getStoreConfig('openerpsync/settings/server_url');
 
54
                
 
55
                # make sure the server url ends with a /
 
56
                if ( substr($server, -1) != "/" )
 
57
                {
 
58
                        $server .= "/";
 
59
                }
 
60
                
 
61
                return $server;
 
62
        }
 
63
 
 
64
        /**
 
65
        * calls OpenERP to place the order
 
66
        * @param array XML/RPC hash of the Magento sale order
 
67
        * @return bool true if the order was created
 
68
        */
 
69
        function createOrder($saleOrder)
 
70
        {
 
71
                if ( $this->debugEnabled() )
 
72
                {
 
73
                        Mage::Log($saleOrder);
 
74
                }
 
75
                
 
76
                try 
 
77
                {
 
78
                        $server = $this->getServerURL();
 
79
                        $database = Mage::getStoreConfig('openerpsync/settings/database');
 
80
                        $userName = Mage::getStoreConfig('openerpsync/settings/api_username');
 
81
                        $password = Mage::getStoreConfig('openerpsync/settings/api_password');
 
82
                        
 
83
                        if ( $this->debugEnabled() )
 
84
                        {
 
85
                                Mage::Log("Server: " . $server);
 
86
                                Mage::Log("Database: " . $database);
 
87
                                Mage::Log("UserName: " . $userName);
 
88
                                Mage::Log("Password: " . $password);
 
89
                        }
 
90
                        
 
91
                        # login to get user id
 
92
                        $conn = new My_XmlRpc_Client($server . "common");
 
93
                        $client = $conn->getProxy();
 
94
                        $userId = $client->login($database, $userName, $password);
 
95
                        
 
96
                        # create the order in OpenERP
 
97
                        $conn = new My_XmlRpc_Client($server . "object");
 
98
                        $client = $conn->getProxy();
 
99
                        $result = $client->execute($database, $userId, $password, "magento.web", "createOrders", array($saleOrder));
 
100
 
 
101
                        if ( $this->debugEnabled() )
 
102
                        {
 
103
                                Mage::Log("Request: " . $conn->getLastRequest()->__toString());
 
104
                                Mage::Log("Response: " . $conn->getLastResponse()->__toString());
 
105
                        }
 
106
                
 
107
                        if ($result['has_error'] > 0 )
 
108
                        {
 
109
                                return false;
 
110
                        }
 
111
                } 
 
112
                catch (Zend_XmlRpc_Exception $e ) 
 
113
                {
 
114
                        Mage::Log("My_XmlRpc_Client execute Failed: " . $e->getMessage());
 
115
                        Mage::getSingleton('checkout/session')->addError($e->getMessage());
 
116
                        return false;
 
117
                }
 
118
                catch (Mage_Core_Exception $e) 
 
119
                {
 
120
                        Mage::Log("Core Exception: " . $e->getMessage());
 
121
                        Mage::getSingleton('checkout/session')->addError($e->getMessage());
 
122
                        return false;
 
123
                }
 
124
                catch (Exception $e) 
 
125
                {
 
126
                        Mage::Log("Exception: " . $e->getMessage());
 
127
                        Mage::getSingleton('checkout/session')->addException(
 
128
                                $e,
 
129
                                Mage::helper('checkout')->__('OpenERP Sync Error')
 
130
                        );
 
131
                        return false;
 
132
                }
 
133
                
 
134
                return true;
 
135
        }
 
136
 
 
137
        /** Grab the Magento sale order with the requested id
 
138
        * @param int sale_order_id int: Magento sale order id
 
139
        * @return array XML/RPC hash of the Magento sale order
 
140
        */
 
141
        function get_sale_order($order) 
 
142
        {               
 
143
                $productArray=array();  // sale order line product wrapper
 
144
 
 
145
                // Magento required models
 
146
                $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
 
147
                
 
148
                // walk the sale order lines
 
149
                foreach ($order->getAllItems() as $item) 
 
150
                {
 
151
                        $productArray[] = array(
 
152
                                "product_sku" => $item->getSku(),
 
153
                                "product_magento_id" => $item->getProductId(),
 
154
                                "product_name" => $item->getName(),
 
155
                                "product_qty" => $item->getQtyOrdered(),
 
156
                                "product_price" => $item->getPrice(),
 
157
                                "product_discount_amount" => $item->getDiscountAmount(),
 
158
                                "product_row_price" => $item->getPrice() - $item->getDiscountAmount(),
 
159
                        );
 
160
                }
 
161
                
 
162
                $streetBA=$order->getBillingAddress()->getStreet();
 
163
                $streetSA=$order->getShippingAddress()->getStreet();
 
164
 
 
165
                $saleorder =  array(
 
166
                        "id" => $order->getId(),
 
167
                        "store_id" => $order->getStoreId(),
 
168
                        "payment"=> $order->getPayment()->getMethod(),
 
169
                        "shipping_amount" => $order->getShippingAmount(),
 
170
                        "grand_total" => $order->getGrandTotal(),
 
171
                        "date"=> $order->getCreatedAt(),
 
172
                        "lines" => $productArray,
 
173
                        "shipping_address" =>    array(
 
174
                                "firstname"=> $order->getShippingAddress()->getFirstname(),
 
175
                                "lastname"=> $order->getShippingAddress()->getLastname(),
 
176
                                "company"=> $order->getShippingAddress()->getCompany(),
 
177
                                "street" => $streetSA[0],
 
178
                                "street2" => (count($streetSA)==2)?$streetSA[1]:'',
 
179
                                "city"=> $order->getShippingAddress()->getCity(),
 
180
                                "postcode"=> $order->getShippingAddress()->getPostcode(),
 
181
                                "country"=> $order->getShippingAddress()->getCountry(),
 
182
                                "phone"=> $order->getShippingAddress()->getTelephone()
 
183
                        ),
 
184
                        "billing_address" =>     array(
 
185
                                "firstname"=> $order->getBillingAddress()->getFirstname(),
 
186
                                "lastname"=> $order->getBillingAddress()->getLastname(),
 
187
                                "company"=> $order->getBillingAddress()->getCompany(),
 
188
                                "street" => $streetBA[0],
 
189
                                "street2" => (count($streetBA)==2)?$streetBA[1]:'',
 
190
                                "city"=> $order->getBillingAddress()->getCity(),
 
191
                                "postcode"=> $order->getBillingAddress()->getPostcode(),
 
192
                                "country"=> $order->getBillingAddress()->getCountry(),
 
193
                                "phone"=> $order->getBillingAddress()->getTelephone()
 
194
                        ),
 
195
                        "customer" => array(
 
196
                                "customer_id"=> $customer->getId(),
 
197
                                "customer_name"=> $customer->getName(),
 
198
                                "customer_email"=> $customer->getEmail()
 
199
                        ),
 
200
                                
 
201
                );
 
202
                
 
203
                return $saleorder;
 
204
        }
 
205
}
 
206
?>