Move busui to seperate repository
[bus.git] / commerceTransactionHandlers.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
<?php
 
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2006 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
 
if(!class_exists('owa_observer')) {
        require_once(OWA_DIR.'owa_observer.php');
}       
require_once(OWA_DIR.'owa_lib.php');
 
/**
 * Commerce Transaction Event handlers
 * 
 * @author      Peter Adams <peter@openwebanalytics.com>
 * @copyright   Copyright &copy; 2006-2011 Peter Adams <peter@openwebanalytics.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
 * @category    owa
 * @package     owa
 * @version             $Revision$            
 * @since               owa 1.4.0
 */
 
class owa_commerceTransactionHandlers extends owa_observer {
        
    /**
     * Notify handler method
     *
     * @param   object $event
     * @access  public
     */
    function notify($event) {
                
                $dispatch = owa_coreAPI::getEventDispatch();
        $ct = owa_coreAPI::entityFactory('base.commerce_transaction_fact');
                $pk = $ct->generateId( $event->get( 'ct_order_id' ) );
                $ct->getByPk( 'id', $pk );
                $id = $ct->get('id'); 
                
                if (!$id) {
                
                        // set missing properties from associated session
                        // this is needed becuase the ecommerce transaction PHP API allows for 
                        // events to be fired into OWA from a non-web request by passing
                        // the session_id.
                        $original_session_id = $event->get( 'original_session_id' );
                        if ( $original_session_id ) {
                                $s = owa_coreAPI::entityFactory( 'base.session' );
                                $s->load( $original_session_id );
                                
                                // override the session id with original session id
                                // this is needed for downstream events
                                $event->set('session_id', $original_session_id);
                                                                
                                if ( $s->get( 'id' ) ) {
                                        $ct->setProperties( $s->_getProperties() );
                                        $ct->set( 'session_id', $original_session_id );
                                } else {
                                        owa_coreAPI::debug('Cannot find original session with id: '.$original_session_id);
                                        return OWA_EHS_EVENT_FAILED;
                                }
                                
                        }
                        
                        $ct->setProperties($event->getProperties());
                        
                        $ct->set( 'id', $pk ); 
                        
                        // Generate Location Id. Location data is comming from user input NOT ip address
                        if ( $event->get( 'country' ) ) {
                                $s = owa_coreAPI::serviceSingleton();
                                $location_id = $s->geolocation->generateId($event->get( 'country' ), $event->get( 'state' ), $event->get( 'city' ) );
                                $ct->set( 'location_id', $location_id );
                        }
                        // set entity properties
                        $ct->set( 'order_id', trim( $event->get( 'ct_order_id' ) ) );
                        $ct->set( 'order_source', trim( strtolower( $event->get( 'ct_order_source' ) ) ) );
                        $ct->set( 'gateway', trim( strtolower( $event->get( 'ct_gateway' ) ) ) );
                        $ct->set( 'total_revenue', owa_lib::prepareCurrencyValue( round( $event->get( 'ct_total' ), 2 ) ) );
                        $ct->set( 'tax_revenue', owa_lib::prepareCurrencyValue( round( $event->get( 'ct_tax' ), 2 ) ) );
                        $ct->set( 'shipping_revenue', owa_lib::prepareCurrencyValue( round( $event->get( 'ct_shipping' ), 2 ) ) );
                        $ct->set( 'days_since_first_session', $event->get('days_since_first_session') );
                        $ct->set( 'num_prior_sessions', $event->get('num_prior_sessions') );
                        
                        $ret = $ct->create();
                        
                        // persist line items
                        if ($ret) {
                                $items = $event->get('ct_line_items');
                                if ( $items ) {
                                        
                                        foreach ($items as $item) {
                                                $ret = $this->persistLineItem($item, $event);
                                        }
                                }
                        }
                        
                        if ($ret) {
                                
                                $sce = $dispatch->makeEvent( 'ecommerce.transaction_persisted' );
                                $sce->setProperties( $event->getProperties() );
                                $dispatch->asyncNotify( $sce );
                        }
                        
                        if ( $ret ) {
                                return OWA_EHS_EVENT_HANDLED;
                        } else {
                                return OWA_EHS_EVENT_FAILED;
                        }
                        
                } else {
                        
                        // dispatch event just incase downstream handlers need to be triggered.
                        $sce = $dispatch->makeEvent( 'ecommerce.transaction_persisted' );
                        $sce->setProperties( $event->getProperties() );
                        $dispatch->asyncNotify( $sce );
                        owa_coreAPI::debug('Not Persisting. Transaction already exists');
                        return OWA_EHS_EVENT_HANDLED;
                }       
    }
    
    
    
    function persistLineItem($item, $parent) {
        
        $ct = owa_coreAPI::entityFactory('base.commerce_line_item_fact');
        $guid = $item['li_order_id'] . $item['li_sku'];
                $pk = $ct->generateId( $guid );
                $ct->getByPk( 'id', $pk );
                $id = $ct->get( 'id' ); 
                
                if ( ! $id ) {
                        
                        $ct->setProperties( $parent->getProperties() );
                        
                        $ct->set( 'id', $pk ); 
                        
                        // Generate Location Id. Location data is comming from user input
                        $ct->set( 'order_id', trim( $item['li_order_id'] ) );
                        $ct->set( 'sku', trim( $item['li_sku'] ) );
                        $ct->set( 'product_name', trim( strtolower( $item['li_product_name'] ) ) );
                        $ct->set( 'category', $item['li_category'] );
                        $ct->set( 'unit_price', owa_lib::prepareCurrencyValue( round($item['li_unit_price'], 2 ) ) );
                        $ct->set( 'quantity', round( $item['li_quantity'] ) );
                        $revenue = round( $item['li_quantity'] * $item['li_unit_price'] , 2 );
                        $ct->set( 'item_revenue', owa_lib::prepareCurrencyValue( $revenue ) );
                        $ret = $ct->create();
                        
                        if ($ret) {
                                return true;
                        } else {
                                return false;
                        }
                        
                } else {
                
                        owa_coreAPI::debug('Not Persisting. line item already exists');
                        return false;
                }       
    }
}
 
?>