@@ -27,69 +27,76 @@ public function index()
2727 public function orderList ($ store_id =null ,Request $ request )
2828 {
2929 \Debugbar::disable ();
30- // Log::debug($request->all());
3130
3231 /**
3332 * get account carts & extract exact store info
3433 */
3534 $ carts = collect ($ this ->api2cart ->getCartList ());
36- $ storeInfo = $ carts ->where ('store_key ' , $ store_id )->first ();
35+
36+ $ totalOrders = 0 ;
37+ $ orders = collect ([]);
38+ $ logs = collect ([]);
3739
3840 $ sort_by = ($ request ->get ('sort_by ' )) ? $ request ->get ('sort_by ' ) : null ;
3941 $ sort_direct = ($ request ->get ('sort_direct ' )) ? true : false ;
4042 $ created_from = ($ request ->get ('created_from ' )) ? Carbon::parse ($ request ->get ('created_from ' ))->format ("Y-m-d\TH:i:sO " ) : null ;
41- $ limit = ($ request ->get ('limit ' )) ? $ request ->get ('limit ' ) : null ;
43+ $ limit = ($ request ->get ('limit ' )) ? (string )$ request ->get ('limit ' ) : null ;
44+ $ length = ($ request ->get ('length ' )) ? $ request ->get ('length ' ) : 15 ;
45+ $ storeKeys = ($ request ->get ('storeKeys ' )) ?: ($ store_id !== null ? [$ store_id ] : []);
4246
43- $ totalOrders = $ this ->api2cart ->getOrderCount ( $ store_id );
4447
45- $ orders = collect ([]);
48+ foreach ($ storeKeys as $ store_id ) {
49+ $ storeInfo = $ carts ->where ('store_key ' , $ store_id )->first ();
50+ $ totalOrders = $ this ->api2cart ->getOrderCount ( $ store_id );
4651
47- //Log::debug( $created_from );
48- if ( $ totalOrders ){
52+ if ( $ totalOrders ) {
4953
50- $ result = $ this ->api2cart ->getOrderList (
51- $ store_id ,
52- null ,
53- null ,
54- null ,
55- $ created_from,
54+ $ result = $ this ->api2cart ->getOrderList (
55+ $ store_id ,
56+ $ sort_by ,
57+ null ,
58+ $ limit ,
59+ $ created_from
5660 );
5761
58- $ newOrders = (isset ($ result ['result ' ]['orders_count ' ])) ? collect ( $ result ['result ' ]['order ' ] ) : collect ([]);
59- // put additional information
60- if ( $ newOrders ->count () ){
61- foreach ($ newOrders as $ item ){
62- $ newItem = $ item ;
63- $ newItem ['cart_id ' ] = $ storeInfo ['cart_id ' ];
64- $ orders ->push ( $ newItem );
62+ $ newOrders = (isset ($ result ['result ' ]['orders_count ' ])) ? collect ( $ result ['result ' ]['order ' ] ) : collect ([]);
63+
64+ // put additional information
65+ if ( $ newOrders ->count () ){
66+ foreach ($ newOrders as $ item ){
67+ $ newItem = $ item ;
68+ $ newItem ['create_at ' ]['value ' ] = Carbon::parse ($ item ['create_at ' ]['value ' ])->setTimezone ('UTC ' )->format ("Y-m-d\TH:i:sO " );
69+ $ newItem ['cart_id ' ] = $ storeInfo ['cart_id ' ];
70+ $ orders ->push ( $ newItem );
71+ }
6572 }
66- }
6773
6874
69- if ( isset ($ result ['pagination ' ]['next ' ]) && strlen ($ result ['pagination ' ]['next ' ]) ){
70- // get next iteration to load rest orders
71- while ( isset ($ result ['pagination ' ]['next ' ]) && strlen ($ result ['pagination ' ]['next ' ]) ){
72- $ result = $ this ->api2cart ->getOrderListPage ( $ store_id , $ result ['pagination ' ]['next ' ]);
73- $ newOrders = (isset ($ result ['result ' ]['orders_count ' ])) ? collect ( $ result ['result ' ]['order ' ] ) : collect ([]);
74- // put additional information
75- if ( $ newOrders ->count () ){
76- foreach ($ newOrders as $ item ){
77- $ newItem = $ item ;
78- $ newItem ['cart_id ' ] = $ storeInfo ['cart_id ' ];
79- $ orders ->push ( $ newItem );
75+ if ( isset ($ result ['pagination ' ]['next ' ]) && strlen ($ result ['pagination ' ]['next ' ]) ){
76+ // get next iteration to load rest orders
77+ while ( isset ($ result ['pagination ' ]['next ' ]) && strlen ($ result ['pagination ' ]['next ' ]) ){
78+ $ result = $ this ->api2cart ->getOrderListPage ( $ store_id , $ result ['pagination ' ]['next ' ]);
79+ $ newOrders = (isset ($ result ['result ' ]['orders_count ' ])) ? collect ( $ result ['result ' ]['order ' ] ) : collect ([]);
80+
81+ // put additional information
82+ if ( $ newOrders ->count () ){
83+ foreach ($ newOrders as $ item ){
84+ $ newItem = $ item ;
85+ $ newItem ['create_at ' ]['value ' ] = Carbon::parse ($ item ['create_at ' ]['value ' ])->setTimezone ('UTC ' )->format ("Y-m-d\TH:i:sO " );
86+ $ newItem ['cart_id ' ] = $ storeInfo ['cart_id ' ];
87+ $ orders ->push ( $ newItem );
88+ }
8089 }
8190 }
91+
8292 }
8393
94+ foreach ($ this ->api2cart ->getLog ()->all () as $ item ) {
95+ $ logs ->push ($ item );
96+ }
8497 }
85-
86-
8798 }
8899
89- // $result = $this->api2cart->getOrderList( $store_id , 'create_at.value', null,$limit);
90- // Log::debug('raw api result');
91- // if ( $store_id == '4730d110180d4b67449f00b44608cb9d' ) Log::debug( print_r($result,1) );
92-
93100 if ( $ sort_by ){
94101 switch ($ sort_by ){
95102 case 'create_at ' :
@@ -104,19 +111,16 @@ public function orderList($store_id=null,Request $request)
104111 $ sorted = $ orders ->sortBy ('create_at.value ' , null , $ sort_direct );
105112 }
106113
107-
108114 $ data = [
109115 "recordsTotal " => $ totalOrders ,
110116 "recordsFiltered " => $ totalOrders ,
111117 "start " => 0 ,
112- "length " => 10 ,
113- "data " => ($ limit ) ? $ sorted ->forPage (0 , $ limit ) : $ sorted ->toArray (),
114-
115- 'log ' => $ this ->api2cart ->getLog (),
118+ "length " => $ length ,
119+ "data " => ($ length ) ? $ sorted ->forPage (0 , $ length ) : $ sorted ->toArray (),
120+ 'log ' => $ logs ,
116121 ];
117122
118123 return response ()->json ($ data );
119-
120124 }
121125
122126
@@ -156,22 +160,17 @@ public function orderProducts($store_id=null,$order_id=null,Request $request)
156160 }
157161
158162 return redirect ( route ('orders.index ' ) );
159-
160163 }
161164
162165 public function abandoned ($ store_id =null , Request $ request )
163166 {
164167 $ items = collect ( $ this ->api2cart ->getAbandonedCart ( $ store_id ) );
165-
166- // Log::debug( print_r($items,1) );
167-
168168 $ data = [
169169 "recordsTotal " => (is_array ($ items )) ? count ($ items ) : 0 ,
170170 "recordsFiltered " => (is_array ($ items )) ? count ($ items ) : 0 ,
171171 "start " => 0 ,
172172 "length " => 10 ,
173173 "data " => collect ($ items ),
174-
175174 'log ' => $ this ->api2cart ->getLog (),
176175 ];
177176
@@ -181,31 +180,31 @@ public function abandoned($store_id=null, Request $request)
181180 public function statuses ($ store_id =null , Request $ request )
182181 {
183182 $ statuses = $ this ->api2cart ->getOrderStatuses ( $ store_id );
183+
184184 if ( !$ statuses ){
185185 return response ()->json (['log ' => $ this ->api2cart ->getLog () ], 404 );
186186 }
187+
187188 if ( $ request ->ajax () ){
188189 return response ()->json (['data ' => collect ($ statuses ['cart_order_statuses ' ]), 'log ' => $ this ->api2cart ->getLog () ]);
189190 }
191+
190192 return redirect ( route ('orders.index ' ) );
191193 }
192194
193195 public function create (Request $ request )
194196 {
195197 $ carts = collect ($ this ->api2cart ->getCartList ());
196198
197- // Log::debug( print_r($carts,1) );
198-
199199 if ( $ request ->ajax () ){
200200 return response ()->json ( ['data ' => view ('orders.form ' , compact ('carts ' ))->render (), 'item ' => $ carts ] );
201201 }
202+
202203 return redirect ( route ('orders.index ' ) );
203204 }
204205
205206 public function store (OrderRequest $ request )
206207 {
207- // Log::debug( print_r($request->all(),1) );
208-
209208 $ cart = $ this ->api2cart ->getCart ( $ request ->get ('cart_id ' ) );
210209 $ customer = $ this ->api2cart ->getCustomer ( $ request ->get ('cart_id ' ), $ request ->get ('customer_id ' ) );
211210
@@ -219,36 +218,28 @@ public function store(OrderRequest $request)
219218 // some customers do not have state
220219 if ( !isset ($ shipping ['state ' ]['code ' ]) || $ shipping ['state ' ]['code ' ] == '' ) $ shipping ['state ' ]['code ' ] = 'AL ' ;
221220
222- // Log::debug( print_r( $address->where('type', 'billing')->first(), 1 ) );
223- // Log::debug( print_r($customer,1) );
224-
225221 $ order = [
226222 'store_id ' => $ cart ['stores_info ' ][0 ]['store_id ' ],
227223 'customer_email ' => $ customer ['email ' ],
228224 'order_status ' => $ request ->get ('status_id ' ),
229225 'subtotal_price ' => 0 ,
230226 'total_price ' => 0 ,
231227
232- 'bill_first_name ' => ( isset ( $ billing ['first_name ' ])) ? $ billing ['first_name ' ] : $ shipping ['first_name ' ],
233- 'bill_last_name ' => ( isset ( $ billing ['last_name ' ])) ? $ billing ['last_name ' ] : $ shipping ['last_name ' ],
234- 'bill_address_1 ' => ( isset ( $ billing ['address1 ' ])) ? $ billing ['address1 ' ] : $ shipping ['address1 ' ],
235- 'bill_city ' => ( isset ( $ billing ['city ' ])) ? $ billing ['city ' ] : $ shipping ['city ' ],
236- 'bill_postcode ' => ( isset ( $ billing ['postcode ' ])) ? $ billing ['postcode ' ] : $ shipping ['postcode ' ],
228+ 'bill_first_name ' => $ billing ['first_name ' ] ?: $ billing ['first_name ' ] ? : $ shipping ['first_name ' ] ?: ' John ' ,
229+ 'bill_last_name ' => $ billing ['last_name ' ] ?: $ billing ['last_name ' ] ? : $ shipping ['last_name ' ] ?: ' Doe ' ,
230+ 'bill_address_1 ' => $ billing ['address1 ' ] ?: $ billing ['address1 ' ] ? : $ shipping ['address1 ' ] ?: ' 221b, Baker street ' ,
231+ 'bill_city ' => $ billing ['city ' ] ?: $ billing ['city ' ] ? : $ shipping ['city ' ] ?: ' London ' ,
232+ 'bill_postcode ' => $ billing ['postcode ' ] ?: $ billing ['postcode ' ] ? : $ shipping ['postcode ' ] ?: ' 00000 ' ,
237233
238234 // state & country need be cleared
239235 'bill_state ' => (isset ($ billing ['state ' ]['code ' ]) && $ billing ['state ' ]['code ' ] != '' ) ? $ billing ['state ' ]['code ' ] : $ shipping ['state ' ]['code ' ],
240- 'bill_country ' => (isset ($ billing ['country ' ]['code3 ' ]) && $ billing ['country ' ]['code3 ' ] != '' ) ? $ billing ['country ' ]['code3 ' ] : $ shipping ['country ' ]['code3 ' ],
241-
242-
243-
236+ 'bill_country ' => $ billing ['country ' ]['code3 ' ] ?: $ billing ['country ' ]['code3 ' ] ?: $ shipping ['country ' ]['code3 ' ] ?: 'UK ' ,
244237 ];
245238
246239 foreach ($ request ->get ('checked_id ' ) as $ cpi ){
247240 $ product = $ this ->api2cart ->getProductInfo ( $ request ->get ('cart_id ' ), $ cpi );
248241 $ quantity = $ request ->get ('product_quantity ' )[ array_search ($ cpi , $ request ->get ('product_id ' )) ];
249242
250- // Log::debug( print_r($product,1));
251-
252243 // check if quantity right
253244 if ( $ product ['quantity ' ]< $ quantity ) continue ;
254245
@@ -264,19 +255,15 @@ public function store(OrderRequest $request)
264255 $ order ['total_price ' ] += $ product ['price ' ] * $ quantity ;
265256 }
266257
267- $ result = $ this ->api2cart ->createOrder ( $ request ->get ('cart_id ' ) , $ order );
268-
269- if ($ result ){
270-
271-
272- return response ()->json ([ 'log ' => $ this ->api2cart ->getLog (), 'item ' => $ this ->api2cart ->getOrderInfo ( $ request ->get ('cart_id ' ), $ result ['order_id ' ] ) ]);
258+ list ($ returnCode , $ result ) = $ this ->api2cart ->createOrder ( $ request ->get ('cart_id ' ) , $ order );
273259
260+ if ($ returnCode == 0 ){
261+ return response ()->json ([ 'log ' => $ this ->api2cart ->getLog (), 'success ' => true , 'item ' => $ this ->api2cart ->getOrderInfo ( $ request ->get ('cart_id ' ), $ result ['order_id ' ] ) ]);
274262 } else {
275263 // error creating order
276- return response ()->json ([ 'log ' => $ this ->api2cart ->getLog () ], 404 );
264+ return response ()->json ([ 'log ' => $ this ->api2cart ->getLog (), ' success ' => false , ' errormessage ' => $ result ] );
277265 }
278266
279-
280267 }
281268
282269}
0 commit comments