Advertisement
vitareinforce

logic import

Dec 23rd, 2022 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. public function import(Request $request) {
  2. $inserted_data = 0;
  3. try {
  4. for($i = 0; $i < count($request->all()); $i++) {
  5. // Header ga dimasukin
  6. if($i > 0) {
  7. $id = $request[$i]['Id'];
  8. $check_id = LegalEntities::find($id);
  9. if($check_id != null) {
  10. $data = LegalEntities::find($id);
  11. $find_director = Employees::find($request[$i]['DirectorId']);
  12. $find_commisioneer = Employees::find($request[$i]['CommissionerId']);
  13. if($request[$i]['Name'] != '' || $request[$i]['Name'] != null) {
  14. $data->Name = $request[$i]['Name'];
  15. } else {
  16. throw new Exception('Kolom Kosong / tidak valid.');
  17. }
  18. if($request[$i]['IsCentral'] == 'Ya') {
  19. $data->IsCentral = 1;
  20. } else if($request[$i]['IsCentral'] == 'Tidak') {
  21. $data->IsCentral = 0;
  22. } else {
  23. throw new Exception('Kolom Kosong / tidak valid.');
  24. }
  25. if($find_director != null) {
  26. $data->DirectorId = $request[$i]['DirectorId'];
  27. } else if($find_director == null) {
  28. $data->DirectorId = null;
  29. }
  30.  
  31. if($find_commisioneer != null) {
  32. $data->CommissionerId = $request[$i]['CommissionerId'];
  33. } else if($find_commisioneer == null) {
  34. $data->CommissionerId = null;
  35. }
  36. $data->save();
  37. } else if($check_id == null) {
  38. $data = new LegalEntities();
  39. $find_director = Employees::find($request[$i]['DirectorId']);
  40. $find_commisioneer = Employees::find($request[$i]['CommissionerId']);
  41. if($request[$i]['Name'] != '' || $request[$i]['Name'] != null) {
  42. $data->Name = $request[$i]['Name'];
  43. } else {
  44. throw new Exception('Kolom Kosong / tidak valid.');
  45. }
  46. if($request[$i]['IsCentral'] == 'Ya') {
  47. $data->IsCentral = 1;
  48. } else if($request[$i]['IsCentral'] == 'Tidak') {
  49. $data->IsCentral = 0;
  50. } else {
  51. throw new Exception('Kolom Kosong / tidak valid.');
  52. }
  53. if($find_director != null) {
  54. $data->DirectorId = $request[$i]['DirectorId'];
  55. } else if($find_director == null) {
  56. $data->DirectorId = null;
  57. }
  58.  
  59. if($find_commisioneer != null) {
  60. $data->CommissionerId = $request[$i]['CommissionerId'];
  61. } else if($find_commisioneer == null) {
  62. $data->CommissionerId = null;
  63. }
  64. $data->save();
  65. }
  66. $inserted_data = $inserted_data + 1;
  67. }
  68. }
  69. return response()->json(['message' => 'Data yang diinput sebanyak ' . $inserted_data]);
  70. } catch (Exception $e) {
  71. return response()->json(['message' => $exception->getMessage()]);
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement