44
55use App \Http \Controllers \Controller ;
66use App \Http \Requests \BusinessCases \AutomaticPriceUpdateRequest ;
7+ use Carbon \Carbon ;
78use Illuminate \Http \Request ;
89use Illuminate \Support \Facades \Log ;
910use Illuminate \Support \Facades \Mail ;
1011
11-
1212use App \Services \Api2Cart ;
1313
1414
@@ -24,139 +24,108 @@ public function __construct(Api2Cart $api2Cart)
2424
2525 public function index ()
2626 {
27-
28- // $result = $this->api2cart->getProductList( "1316ad9a66ac871ce46a3d59005acc9c", null, null, null, null , null );
29- // session()->put('automatic_price_updating', collect( $result['result']['product'] ) );
30- //
31- // $products = session()->get('automatic_price_updating');
32- //
33- // print_r($products);
34-
3527 return view ('business_cases.automatic_price_updating.index ' );
3628 }
3729
3830
3931 public function create (Request $ request )
4032 {
4133 if ( $ request ->ajax () ){
42- return response ()->json (['data ' => view ('business_cases.automatic_price_updating.form ' )->render (), 'log ' => $ this ->api2cart ->getLog () ]);
34+ $ carts = collect ($ this ->api2cart ->getCartList ());
35+ $ isCreate = true ;
36+ return response ()->json (['data ' => view ('business_cases.automatic_price_updating.form ' , compact ('isCreate ' , 'carts ' ))->render (), 'log ' => $ this ->api2cart ->getLog () ]);
4337 }
4438
4539 return redirect (route ('business_cases.automatic_price_updating ' ));
4640 }
4741
4842 public function store (AutomaticPriceUpdateRequest $ request )
4943 {
50- Log::debug ( $ request ->all () );
51-
52- $ products = [];
53- $ carts = collect ($ this ->api2cart ->getCartList ());
54-
5544 $ formData = $ request ->except ('_hash ' );
5645 $ formData ['model ' ] = $ formData ['sku ' ];
5746
58- foreach ($ carts as $ item ){
59-
60- $ result = $ this ->api2cart ->addProduct ($ item ['store_key ' ], $ formData );
47+ list ($ returnCode , $ result ) = $ this ->api2cart ->addProduct ($ formData ['cart_id ' ], $ formData );
6148
62- // add image for each product
63- if ( isset ($ result ['result ' ]['product_id ' ]) && $ result ['result ' ]['product_id ' ] != '' ){
49+ if ($ returnCode == 0 ) {
50+ // add image to product
51+ if ( isset ($ result ['result ' ]['product_id ' ]) && $ result ['result ' ]['product_id ' ] != '' ) {
6452
65- $ this ->api2cart ->addProductImage ( $ item [ ' store_key ' ], $ result ['result ' ]['product_id ' ],
53+ $ this ->api2cart ->addProductImage ($ formData [ ' cart_id ' ], $ result ['result ' ]['product_id ' ],
6654 // image data goes here
6755 [
6856 'type ' => 'base ' ,
6957 'image_name ' => '0011ff.png ' ,
7058 'url ' => 'https://dummyimage.com/600x400/ffffff/0011ff.png '
7159 ]
7260 );
73-
74- }
75-
76- if ($ result ['result ' ]['product_id ' ] != '' ){
77- $ products [] = [
78- 'store_key ' => $ item ['store_key ' ],
79- 'product_id ' => $ result ['result ' ]['product_id ' ],
80- 'sku ' => $ request ->get ('sku ' ),
81- ];
8261 }
8362
63+ return response ()->json ([ 'log ' => $ this ->api2cart ->getLog (), 'success ' => true , 'item ' => $ this ->api2cart ->getProductInfo ( $ formData ['cart_id ' ], $ result ['ptoduct_id ' ] ) ]);
64+ } else {
65+ return response ()->json ([ 'log ' => $ this ->api2cart ->getLog (), 'success ' => false , 'errormessage ' => $ result ]);
8466 }
85-
86-
87- // Log::debug( print_r($products,1) );
88-
89- session ()->put ('automatic_price_updating ' , $ products );
90-
91- return response ()->json (['items ' => $ products ,'log ' => $ this ->api2cart ->getLog () ]);
92-
93-
9467 }
9568
9669
9770 public function products (Request $ request )
9871 {
99- $ products = [];
100- $ products_ids = session ()->get ('automatic_price_updating ' );
101-
102- // Log::debug( print_r($products_ids,1) );
103-
104- if ( !$ products_ids ) return response (null , 404 );
105-
106- foreach ($ products_ids as $ item ){
107- $ tmp = $ this ->api2cart ->getProductInfo ( $ item ['store_key ' ], $ item ['product_id ' ] );
108- if ($ tmp ) {
109- $ tmp ['store_key ' ] = $ item ['store_key ' ];
110- $ products [] = $ tmp ;
72+ $ products = collect ([]);
73+ $ limit = 5 ;
74+ $ carts = $ request ->get ('store_keys ' , []);
75+
76+ foreach ($ carts as $ storeKey ) {
77+ $ productsInfo = $ this ->api2cart ->getProductList ($ storeKey , null , 'create_at ' , 'desc ' , $ limit );
78+
79+ if ($ productsInfo ) {
80+ foreach ($ productsInfo ['result ' ]['product ' ] ?? [] as $ productInfo ) {
81+ $ productInfo ['store_key ' ] = $ storeKey ;
82+ $ productInfo ['create_at ' ]['value ' ] = Carbon::parse ($ productInfo ['create_at ' ]['value ' ])->setTimezone ('UTC ' )->format ("Y-m-d\TH:i:sO " );
83+ $ products ->push ($ productInfo );
84+ }
11185 }
11286 }
11387
114- // Log::debug( print_r($products,1) );
115-
88+ $ items = [];
11689
117- return response ()->json (['items ' =>$ products ]);
90+ foreach ($ products ->sortBy ('create_at.value ' , null , true )->toArray () as $ item ) {
91+ $ items [] = $ item ;
92+ }
11893
94+ return response ()->json (['items ' => $ items ]);
11995 }
12096
12197 public function edit (Request $ request )
12298 {
12399 $ this ->api2cart ->debug = false ;
100+ $ isCreate = false ;
101+ $ storeKey = $ request ->get ('store_key ' , '' );
102+ $ productId = $ request ->get ('id ' , '' );
124103
125- $ products = [];
126- $ products_ids = session ()->get ('automatic_price_updating ' );
104+ $ product = [];
127105
128- foreach ($ products_ids as $ item ){
129- $ tmp = $ this ->api2cart ->getProductInfo ( $ item ['store_key ' ], $ item ['product_id ' ] );
130- if ($ tmp ) {
131- $ tmp ['store_key ' ] = $ item ['store_key ' ];
132- $ products [] = $ tmp ;
133- }
134- }
106+ if ($ storeKey && $ productId ) {
107+ $ res = $ this ->api2cart ->getProductInfo ($ storeKey , $ productId );
135108
136- if ( count ($ products ) != count ($ products_ids ) ){
137- // looks stores reseting
138- // Log::debug( print_r($products,1) );
139- session ()->flash ('automatic_price_updating ' );
140- return response ('Please create new product scope ' ,404 );
109+ if ($ res ) {
110+ $ res ['store_key ' ] = $ storeKey ;
111+ $ product = $ res ;
112+ }
141113 }
142114
143- $ product = $ products [0 ];
144-
145115 if ( $ request ->ajax () ){
146- return response ()->json (['data ' => view ('business_cases.automatic_price_updating.form ' , compact ('products ' ,'product ' ))->render (), 'log ' => $ this ->api2cart ->getLog () ]);
116+ return response ()->json ([
117+ 'data ' => view ('business_cases.automatic_price_updating.form ' , compact ('productId ' , 'storeKey ' , 'product ' , 'isCreate ' ))->render (),
118+ 'log ' => $ this ->api2cart ->getLog ()
119+ ]);
147120 }
148121 }
149122
150123
151124 public function update (AutomaticPriceUpdateRequest $ request )
152125 {
153- $ products_ids = session ()->get ('automatic_price_updating ' );
154126 $ formData = $ request ->except ('_token ' ,'_method ' );
155- $ formData ['model ' ] = $ formData ['sku ' ];
156127
157- foreach ($ products_ids as $ item ){
158- $ this ->api2cart ->updateProduct ( $ item ['store_key ' ], $ item ['product_id ' ], $ formData );
159- }
128+ $ this ->api2cart ->updateProduct ($ formData ['cart_id ' ], $ formData ['product_id ' ], $ formData );
160129
161130 return response ()->json (['log ' => $ this ->api2cart ->getLog ()]);
162131
0 commit comments