Skip to content

Commit 7633e36

Browse files
Taras KubivIgor Liubarskyi
authored andcommitted
Merged in APITOCART-16378-demo-moduleimport-orders (pull request #38)
APITOCART-16378-demo-moduleimport-orders [MASTER] Approved-by: Oleg Syvak
2 parents 83c05aa + 51621a7 commit 7633e36

File tree

8 files changed

+480
-455
lines changed

8 files changed

+480
-455
lines changed

run_me.sh

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ docker-compose down
4141
#run containers in background
4242
docker-compose up -d
4343

44-
if [ $? -eq 0 ];
45-
then
46-
echo "Looks all ok, update related dependencies..."
47-
docker-compose run app composer update
48-
docker-compose run app php artisan migrate
49-
docker-compose run app php artisan db:seed
50-
else
51-
echo "missing start please contact support"
52-
exit
53-
fi
44+
#run once for the first time
45+
#if [ $? -eq 0 ];
46+
#then
47+
# echo "Looks all ok, update related dependencies..."
48+
# docker-compose run app composer update
49+
# docker-compose run app php artisan migrate
50+
# docker-compose run app php artisan db:seed
51+
#else
52+
# echo "missing start please contact support"
53+
# exit
54+
#fi
5455

5556
if ! [ -x "$(command -v xdg-open)" ]; then
5657
open https://demo-module.api2cart.local.com:8443

src/app/Http/Controllers/OrdersController.php

Lines changed: 62 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/app/Http/Controllers/StoresController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Services\Api2Cart;
77
use Illuminate\Http\Request;
88
use Illuminate\Support\Facades\Log;
9+
use Cache;
910

1011
class StoresController extends Controller
1112
{
@@ -34,20 +35,23 @@ public function storeList(Request $request)
3435
* get account carts & avialable carts
3536
*/
3637
$carts = collect($this->api2cart->getCartList());
37-
$allCarts = collect($this->api2cart->getCartsList());
38-
39-
// Log::debug( print_r($allCarts,1));
40-
// if ( !$carts->count() || !$allCarts->count() ) return response()->json([],404);
4138

39+
$allCarts = Cache::remember('allCarts', 3600, function () {
40+
return collect($this->api2cart->getCartsList());
41+
});
4242

4343
$result = $carts->map(function ($store) use ($allCarts) {
44-
$info = $this->api2cart->getCart( $store['store_key'] );
44+
$info = Cache::remember('cart_' . $store['store_key'], 3600, function () use ($store) {
45+
return $this->api2cart->getCart( $store['store_key'] );
46+
});
47+
4548
// put additional info
4649
$store['stores_info']['store_owner_info'] = [
4750
'owner' => ( isset($info['stores_info'][0]['store_owner_info']['owner']) ) ? $info['stores_info'][0]['store_owner_info']['owner'] : null,
4851
'email' => ( isset($info['stores_info'][0]['store_owner_info']['email']) ) ? $info['stores_info'][0]['store_owner_info']['email'] : null
4952
];
5053
$store['cart_info'] = $allCarts->where('cart_id', $store['cart_id'])->first();
54+
5155
return $store;
5256
});
5357

src/app/Services/Api2Cart.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -776,25 +776,20 @@ public function createOrder( $store_id=null, $fields=null )
776776
$fields
777777
);
778778

779-
// Log::debug( print_r($result,1) );
780-
781779
$this->logApiCall( 'order.add.json', $result->getReturnCode(), $this->order->getConfig(), null, null, null, $result->getReturnMessage() , $fields );
782780

783781
if ( $result->getReturnCode() == 0 ){
784-
return $this->mapToArray( $result->getResult() );
782+
return [0, $this->mapToArray( $result->getResult() )];
785783
} else {
786784
if ($this->debug) Log::debug( print_r($result,1) );
787-
return null;
785+
return [$result->getReturnCode(), $result->getReturnMessage()];
788786
}
789787

790-
791788
} catch (\Exception $e){
792-
793789
Log::debug( $e->getMessage() );
794790
$this->logApiCall( 'order.add.json', $e->getCode(), $this->account->getConfig(), null, null, null, $e->getMessage() , $fields );
795791
return false;
796792
}
797-
798793
}
799794

800795
public function getProductCount($store_id=null)

0 commit comments

Comments
 (0)