argument('id'); $record = DB::table('csv_imports_processes')->where('id', $id)->first(); $data = file(base_path('public/' . $record->csv_path)); $data_count = count($data); $counter = 0; $price_list_category = ""; foreach($data as $item) { $split_item = explode(',', $item); $item_id = 0; // check if this is the first row and get ids if ($counter == 0) { $category_id_arr = []; for ($i = 1; $i < count($split_item); $i++){ $category_id = get_name(get_name(trim($split_item[$i]), 'name', 'id', 'patient_categories'), 'patient_category_id', 'id', 'price_list_categories'); $category_id_arr[] = $category_id != 'N/A' ? $category_id : 0; } $price_list_category = implode(",", $category_id_arr); $counter++; continue; } else { $category_price = []; // get the item id if ($record->tag_id == 3) { $item_id = get_name($split_item[0], 'name', 'id', 'drugs'); } else if ($record->tag_id == 2) { $item_id = get_name($split_item[0], 'name', 'id', 'investigations'); } else if ($record->tag_id == 4) { $item_id = get_name($split_item[0], 'name', 'id', 'procedures'); } else if ($record->tag_id == 5) { $item_id = get_name($split_item[0], 'name', 'id', 'sundries'); } else if ($record->tag_id == 6) { $item_id = get_name($split_item[0], 'name', 'id', 'services'); } if (!is_numeric($item_id)) { continue; } for ($i = 1; $i < count($split_item); $i++){ try { // catch non numeric error for some cases $category_price[] = +trim($split_item[$i]); } catch (\Exception $exception) { $category_price[] = 0; } } $price_list_price = implode(",", $category_price); } if ($record->tag_id == 3) { try { DB::table('drugs')->where('id', $item_id)->update([ "price_list_category" => $price_list_category, "price_list_price" => $price_list_price, "updated_at" => now() ]); } catch (\Exception $exception) {} } else if ($record->tag_id == 2) { try { DB::table('investigations')->where('id', $item_id)->update([ "price_list_category" => $price_list_category, "price_list_price" => $price_list_price, "updated_at" => now() ]); } catch (\Exception $exception) {} } else if ($record->tag_id == 4) { try { DB::table('procedures')->where('id', $item_id)->update([ "price_list_category" => $price_list_category, "price_list_price" => $price_list_price, "updated_at" => now() ]); } catch (\Exception $exception) {} } else if ($record->tag_id == 5) { try { DB::table('sundries')->where('id', $item_id)->update([ "price_list_category" => $price_list_category, "price_list_price" => $price_list_price, "updated_at" => now() ]); } catch (\Exception $exception) {} } else if ($record->tag_id == 6) { try { DB::table('services')->where('id', $item_id)->update([ "price_list_category" => $price_list_category, "price_list_price" => $price_list_price, "updated_at" => now() ]); } catch (\Exception $exception) {} } $counter++; $percentage = round((($counter / $data_count) * 100)); $is_run_complete = 0; if ($counter == $data_count) { $is_run_complete = 1; } elseif ($percentage == 100) { // because of the round(), 100% is reached before completion $percentage = 99; } DB::table('csv_imports_processes') ->where('id', $id) ->update([ "records_completed" => $percentage, "is_complete" => $is_run_complete ]); } } }