Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function doImportChunk(Request $request)
- {
- try{
- $file_md5 = md5($request->get('file'));
- if ($request->get('file') && $request->get('resume') == 1) {
- $total = Session::get('total_data_import');
- $prog = $total > 0 ? intval(Cache::get('success_' . $file_md5)) / $total * 100 : 0;
- $prog = round($prog, 2);
- if ($prog >= 100) {
- Cache::forget('success_' . $file_md5);
- }
- return response()->json(['progress' => $prog, 'last_error' => Cache::get('error_' . $file_md5)]);
- }
- $select_column = Session::get('select_column');
- $select_column = array_filter($select_column);
- $table_columns = [
- 'brand_name',
- 'brand_logo'
- ];
- $file = base64_decode($request->get('file'));
- $file = storage_path('app/' . $file);
- $user_id = CRUDBooster::myParentId();
- $type = pathinfo($file, PATHINFO_EXTENSION);
- $import = new AdminDrmImportsController;
- $rows = $import->csvToArray($file, $type, 'auto', false);
- $count = 0;
- $existing_brands = ProductBrand::where('user_id', $user_id)->pluck('brand_name')->toArray();
- $new_brand = [];
- try {
- foreach ($rows as $row) {
- $count++;
- Cache::put('success_' . $file_md5, $count);
- $tmp_data = [];
- foreach ($table_columns as $key => $value) {
- if($value == 'brand_name'){
- $tmp_data[$value] = trim( $row[$select_column[$key]] );
- }else if($value == 'brand_logo'){
- $tmp_data[$value] = !empty($select_column[$key]) ? json_encode([$row[$select_column[$key]]]) : ["https://drm.software/Marketing_assets/img/no-product-image.jpg"];
- }
- }
- $tmp_data['user_id'] = $user_id;
- $tmp_data['created_at'] = Carbon::now()->toDateTimeString();
- $tmp_data['updated_at'] = Carbon::now()->toDateTimeString();
- $new_brand[] = $tmp_data;
- }
- $brands_unique_name = array_unique(array_map('strtolower', array_column($new_brand, 'brand_name')));
- $new_brand = array_intersect_key($new_brand, $brands_unique_name);
- $new_brands = array_filter($new_brand, function ($value) use ($existing_brands) {
- return !in_array(strtolower($value['brand_name']), array_map('strtolower', $existing_brands));
- });
- $new_brands = array_values($new_brands);
- $existing_brandss = ProductBrand::select('brand_name','brand_logo')->whereIn('brand_name', $rows)->get();
- // dd($new_brands[$key]['brand_name']);
- if($new_brands){
- collect($new_brands)
- ->chunk(500)
- ->each(function($brand_chunk) use($user_id){
- $brand_chunk = $brand_chunk->toArray();
- if(CRUDBooster::isSupplier()){
- ProductBrand::insert($brand_chunk);
- foreach ($brand_chunk as $value) {
- $brand_name = $value['brand_name'];
- $brand_id = ProductBrand::where('brand_name', $brand_name)->value('id');
- //dd($brand_id);
- SupplierBrand::updateOrCreate(['user_id' => $user_id, 'brand_id' => $brand_id], [
- 'brand_logo' =>$value['brand_logo']
- ]);
- }
- }
- else{
- ProductBrand::insert($brand_chunk);
- }
- });
- } if(CRUDBooster::isSupplier() && !empty($existing_brandss)){
- $checkSupplierHasBrand = SupplierBrand::select('brand_id')->where('user_id', $user_id)->get()->toArray();
- foreach ($existing_brandss as $brand) {
- $brand_id = ProductBrand::where('brand_name', $brand->brand_name)->value('id');
- if(!in_array($brand_id, $checkSupplierHasBrand)){
- SupplierBrand::updateOrCreate(['user_id' => $user_id, 'brand_id' => $brand_id], [
- 'brand_logo' => $brand->brand_logo
- ]);
- }
- }
- }
- return response()->json(['status' => true, 'message' => "Brand Imported !!!"]);
- }catch(\Exception $e){
- $e = (string) $e;
- Cache::put('error_' . $file_md5, $e, 500);
- return response()->json(['status' => false, 'message' => $e]);
- }
- }catch (\Exception $e) {
- return response()->json(['status' => false, 'message' => "Sorry, something went wrong"]);
- }
- }
Add Comment
Please, Sign In to add comment