1
+ <?php
2
+ namespace Sequra \PhpClient ;
3
+ /**
4
+ * Class BuilderAbstract
5
+ */
6
+ abstract class BuilderAbstract
7
+ {
8
+
9
+ protected $ merchant_id ;
10
+ protected $ _current_order = null ;
11
+ protected $ _orders = array ();
12
+ protected $ _broken_orders = array ();
13
+ protected $ _delivery_report = null ;
14
+ protected $ _building_report = false ;
15
+
16
+ public function __construct ($ merchant_id )
17
+ {
18
+ $ this ->merchant_id = $ merchant_id ;
19
+ }
20
+
21
+ public function build ($ state = null )
22
+ {
23
+ $ order = array (
24
+ 'merchant ' => $ this ->merchant (),
25
+ 'cart ' => $ this ->cartWithItems (),
26
+ 'delivery_address ' => $ this ->deliveryAddress (),
27
+ 'invoice_address ' => $ this ->invoiceAddress (),
28
+ 'customer ' => $ this ->customer (),
29
+ 'gui ' => $ this ->gui (),
30
+ 'platform ' => $ this ->platform (),
31
+ 'state ' => self ::notNull ($ state )
32
+ );
33
+ $ order = $ this ->fixTotals ($ order );
34
+ $ reference = $ this ->orderMerchantReference ();
35
+ if ('confirmed ' == $ state && count ($ reference )>0 )
36
+ $ order ['merchant_reference ' ] = $ reference ;
37
+
38
+ return $ order ;
39
+ }
40
+
41
+ public function fixTotals ($ order )
42
+ {
43
+ $ totals = self ::totals ($ order ['cart ' ]);
44
+ $ diff_without_tax = $ order ['cart ' ]['order_total_without_tax ' ] - $ totals ['without_tax ' ];
45
+ $ diff_with_tax = $ order ['cart ' ]['order_total_with_tax ' ] - $ totals ['with_tax ' ];
46
+ $ diff_max = abs (max ($ diff_with_tax , $ diff_without_tax ));
47
+ /*Dont correct error bigger than Itm count cents*/
48
+ if ($ diff_max == 0 || abs ($ diff_max ) > count ($ order ['cart ' ]['items ' ]))
49
+ return $ order ;
50
+ $ items = array ();
51
+ $ amended = false ;
52
+ foreach ($ order ['cart ' ]['items ' ] as $ item ) {
53
+ if ('discount ' == $ item ['type ' ]) {
54
+ $ item ['total_without_tax ' ] += $ diff_without_tax ;
55
+ $ item ['total_with_tax ' ] += $ diff_with_tax ;
56
+ $ item ['name ' ] .= ('' ==$ item ['name ' ]?'' :' + ' ) . 'ajuste por redondeos ' ;
57
+ $ amended = true ;
58
+ }
59
+ $ items [] = $ item ;
60
+ }
61
+ if (!$ amended ){
62
+ unset($ item );
63
+ $ item ['type ' ] = 'discount ' ;
64
+ $ item ['reference ' ] = '' ;
65
+ $ item ['name ' ] = 'ajuste por redondeos ' ;
66
+ $ item ['total_without_tax ' ] = $ diff_without_tax ;
67
+ $ item ['total_with_tax ' ] = $ diff_with_tax ;
68
+ $ items [] = $ item ;
69
+ }
70
+
71
+ $ order ['cart ' ]['items ' ] = $ items ;
72
+ return $ order ;
73
+ }
74
+
75
+ public function buildDeliveryReport ()
76
+ {
77
+ $ this ->_building_report = true ;
78
+ $ this ->buildShippedOrders ();
79
+ $ this ->buildBrokenOrders ();
80
+ $ this ->_delivery_report = array (
81
+ 'merchant ' => $ this ->merchant (),
82
+ 'orders ' => $ this ->_orders ,
83
+ 'broken_orders ' => $ this ->_broken_orders ,
84
+ 'statistics ' => array ('orders ' => $ this ->getOrderStats ()),
85
+ 'platform ' => $ this ->platform ()
86
+ );
87
+ }
88
+
89
+ public function getDeliveryReport ()
90
+ {
91
+ if (is_null ($ this ->_delivery_report ))
92
+ $ this ->buildDeliveryReport ();
93
+ return $ this ->_delivery_report ;
94
+ }
95
+
96
+ public function merchant ()
97
+ {
98
+ return array (
99
+ 'id ' => $ this ->merchant_id ,
100
+ );
101
+ }
102
+
103
+ public abstract function buildShippedOrders ();
104
+
105
+ function buildBrokenOrders ()
106
+ {
107
+ foreach ($ this ->_orders as $ key => $ order ) {
108
+ if (!self ::isConsistentCart ($ order ['cart ' ])) {
109
+ $ this ->_broken_orders [] = $ order ;
110
+ unset($ this ->_orders [$ key ]);
111
+ }
112
+ }
113
+ }
114
+
115
+ public function orderMerchantReference ()
116
+ {
117
+ $ ret = array ();
118
+ $ ref = self ::notNull ($ this ->getOrderRef (1 ));
119
+ if ('' !=$ ref )
120
+ $ ret ['order_ref_1 ' ] = $ ref ;
121
+ $ ref = self ::notNull ($ this ->getOrderRef (2 ));
122
+ if ('' !=$ ref )
123
+ $ ret ['order_ref_2 ' ] = $ ref ;
124
+ return $ ret ;
125
+ }
126
+ /*
127
+ * Get order_ref_n to send to sequra
128
+ */
129
+ public abstract function getOrderRef ($ i );
130
+
131
+ public abstract function getOrderStats ();
132
+
133
+ public abstract function cartWithItems ();
134
+
135
+ public abstract function deliveryMethod ();
136
+
137
+ public function items ()
138
+ {
139
+ return array_merge (
140
+ $ this ->productItem (),
141
+ $ this ->extraItems (),
142
+ $ this ->handlingItems ()
143
+ );
144
+ }
145
+
146
+ public abstract function handlingItems ();
147
+
148
+ public abstract function customer ();
149
+
150
+ protected static function dateOrBlank ($ date )
151
+ {
152
+ return $ date ? date_format (date_create ($ date ), 'Y-m-d ' ) : '' ;
153
+ }
154
+
155
+ public static function isConsistentCart ($ cart )
156
+ {
157
+ $ totals = self ::totals ($ cart );
158
+ return $ cart ['order_total_without_tax ' ] == $ totals ['without_tax ' ] && $ cart ['order_total_with_tax ' ] == $ totals ['with_tax ' ];
159
+ }
160
+
161
+ public static function totals ($ cart )
162
+ {
163
+ $ total_without_tax = $ total_with_tax = 0 ;
164
+ foreach ($ cart ['items ' ] as $ item ) {
165
+ $ total_without_tax += $ item ['total_without_tax ' ];
166
+ $ total_with_tax += $ item ['total_with_tax ' ];
167
+ }
168
+ return array ('without_tax ' => $ total_without_tax , 'with_tax ' => $ total_with_tax );
169
+ }
170
+
171
+ public static abstract function getPreviousOrders ($ customerID );
172
+
173
+ public function gui ()
174
+ {
175
+ $ data = array (
176
+ 'layout ' => $ this ->isMobile () ? 'mobile ' : 'desktop ' ,
177
+ );
178
+ return $ data ;
179
+ }
180
+ public static abstract function platform ();
181
+
182
+ static $ centsPerWhole = 100 ;
183
+
184
+ public static function integerPrice ($ price )
185
+ {
186
+ return intval (round (self ::$ centsPerWhole * $ price ));
187
+ }
188
+
189
+ protected static function notNull ($ value1 )
190
+ {
191
+ return is_null ($ value1 ) ? '' : $ value1 ;
192
+ }
193
+
194
+ // TODO: find out were this method was copied from so that we can see when it is updated.
195
+ protected static function isMobile ()
196
+ {
197
+ $ regex_match = "/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link| "
198
+ . "htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource| "
199
+ . "blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui| "
200
+ . "symbian|smartphone|mmp|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm| "
201
+ . "jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220 "
202
+ . ")/i " ;
203
+
204
+ if (preg_match ($ regex_match , strtolower ($ _SERVER ['HTTP_USER_AGENT ' ]))) {
205
+ return true ;
206
+ }
207
+
208
+ if ((strpos (strtolower ($ _SERVER ['HTTP_ACCEPT ' ]), 'application/vnd.wap.xhtml+xml ' ) > 0 ) or ((isset ($ _SERVER ['HTTP_X_WAP_PROFILE ' ]) or isset ($ _SERVER ['HTTP_PROFILE ' ])))) {
209
+ return true ;
210
+ }
211
+
212
+ $ mobile_ua = strtolower (substr ($ _SERVER ['HTTP_USER_AGENT ' ], 0 , 4 ));
213
+ $ mobile_agents = array (
214
+ 'w3c ' , 'acs- ' , 'alav ' , 'alca ' , 'amoi ' , 'audi ' , 'avan ' , 'benq ' , 'bird ' , 'blac ' ,
215
+ 'blaz ' , 'brew ' , 'cell ' , 'cldc ' , 'cmd- ' , 'dang ' , 'doco ' , 'eric ' , 'hipt ' , 'inno ' ,
216
+ 'ipaq ' , 'java ' , 'jigs ' , 'kddi ' , 'keji ' , 'leno ' , 'lg-c ' , 'lg-d ' , 'lg-g ' , 'lge- ' ,
217
+ 'maui ' , 'maxo ' , 'midp ' , 'mits ' , 'mmef ' , 'mobi ' , 'mot- ' , 'moto ' , 'mwbp ' , 'nec- ' ,
218
+ 'newt ' , 'noki ' , 'oper ' , 'palm ' , 'pana ' , 'pant ' , 'phil ' , 'play ' , 'port ' , 'prox ' ,
219
+ 'qwap ' , 'sage ' , 'sams ' , 'sany ' , 'sch- ' , 'sec- ' , 'send ' , 'seri ' , 'sgh- ' , 'shar ' ,
220
+ 'sie- ' , 'siem ' , 'smal ' , 'smar ' , 'sony ' , 'sph- ' , 'symb ' , 't-mo ' , 'teli ' , 'tim- ' ,
221
+ 'tosh ' , 'tsm- ' , 'upg1 ' , 'upsi ' , 'vk-v ' , 'voda ' , 'wap- ' , 'wapa ' , 'wapi ' , 'wapp ' ,
222
+ 'wapr ' , 'webc ' , 'winw ' , 'winw ' , 'xda ' , 'xda- ' );
223
+
224
+ if (in_array ($ mobile_ua , $ mobile_agents )) {
225
+ return true ;
226
+ }
227
+
228
+ if (isset ($ _SERVER ['ALL_HTTP ' ]) && strpos (strtolower ($ _SERVER ['ALL_HTTP ' ]), 'OperaMini ' ) > 0 ) {
229
+ return true ;
230
+ }
231
+
232
+ return false ;
233
+ }
234
+ }
0 commit comments