role != 3) { array_push($data['tableHead'],["Act","all","action"]); } $data['db'] = $this->table; $data['db_key'] = $this->primaryKey; $data['route'] = $route; $dtable = \DB::table($this->table) ->select("merchant.*", DB::raw("string_agg(DISTINCT reff_tax.tax_nm || ' (' || reff_tax.tax_prs || '%)' ,', ') as tax_list")) ->leftJoin("device", function($join){ $join->on("device.mid", "=", "merchant.mid"); }) ->leftJoin("merchant_tax", function($join){ $join->on("merchant_tax.mid", "=", "merchant.mid"); }) ->leftJoin("reff_tax", function($join){ $join->on("reff_tax.tax_id", "=", "merchant_tax.tax_id"); }) ->groupBy("merchant.mid") ->orderBy('merchant_nm') ->when(Auth::user()->role == 3, function ($query) use ($request) { $query->where('city_id', Auth::user()->id_kota); }) ->get(); // LIST DATA TABLE $data['data_table'] = $dtable; // FORM FIELD FOR STORE $data['set_field'] = [ // 'is_active' => $request->post('is_active') ]; // GET DATA FOR EDIT if ($request->post('id')) { if (Auth::user()->role == 3) { return response()->json([ 'rc' => 99, 'rm' => "No Access" ]); } $data['get_data_edit']['merchant'] = \DB::selectOne("SELECT * from merchant m where m.mid = ?",[$request->post('id')]); } foreach($data['tableHead'] as $v){ $arrHead[] = $v[2]; } $data['head'] = implode(",",$arrHead); return $data; } public function getDT($data,$init) { $dt = DT::of($data); $dt->editColumn('is_hotel',function($data) { return ($data->is_hotel == true) ? '':''; }); $dt->editColumn('is_resto',function($data) { return ($data->is_resto == true) ? '':''; }); $dt->editColumn('tax_list',function($data) { $list = ''; $dList = explode(",",$data->tax_list); if($data->tax_list){ foreach ($dList as $key => $v) { $list .= ''.$v.''; } } return $list; }); $dt->editColumn('logo',function($data) { return ''; }); return $dt; } public function storeCustom($act,Request $request) { DB::beginTransaction(); try{ $cGlobal = new Controller(); $titleTrail = 'Merchant'; switch ($act) { case 'storeMerchant': if (Auth::user()->role == 3) { return response()->json([ 'rc' => 99, 'rm' => "No Access" ]); } $id = $request->input('get_id'); $oldData = null; // VALIDATE if ($id == '') { $checkMID = DB::table('merchant')->where('mid', $request->mid)->count(); // $checkNMID = DB::table('merchant')->where('nmid', $request->nmid)->count(); $checkPHONE = DB::table('merchant')->where('phone_no', $request->phone_no)->count(); if ($checkMID > 0) { return response()->json([ 'rc' => 99, 'rm' => "MID ".$request->mid." Sudah Terdaftar" ]); } if ($checkPHONE > 0) { return response()->json([ 'rc' => 99, 'rm' => "Phone No ".$request->phone_no." Sudah Terdaftar" ]); } }else{ $dataCheck = DB::table('merchant')->where('mid',$id)->first(); if ($request->phone_no == $dataCheck->phone_no) { }else{ $checkPHONE = DB::table('merchant')->where('phone_no',$request->phone_no)->count(); $validPHONE = ($checkPHONE > 0) ? false : true; if ($validPHONE == false) { return response()->json([ 'rc' => 99, 'rm' => "Phone No ".$request->phone_no." Sudah Terdaftar" ]); } } } if ($id == "") { $event = "Tambah ".$titleTrail." ".$request->input('merchant_nm'); $datas = new merchantModel(); $datas->mid = $cGlobal->cleanString($request->mid); }else{ $event = "Ubah ".$titleTrail." ".$request->input('merchant_nm'); $oldData = \DB::table($this->table)->where('mid',$id)->first(); $datas = merchantModel::find($id); } $datas->merchant_nm = $cGlobal->cleanString($request->merchant_nm); $datas->address = $request->address; $datas->phone_no = $request->phone_no; $datas->nmid = $cGlobal->cleanString($request->nmid); $datas->is_hotel = $request->is_hotel; $datas->is_resto = $request->is_resto; // $datas->email = $request->email; $datas->city_id = $request->kota; $datas->save(); $cGlobal->auditTrailValue($event,$titleTrail,$this->table,json_encode($datas),json_encode($oldData)); break; case 'listTax': if (Auth::user()->role == 3) { return response()->json([ 'rc' => 99, 'rm' => "No Access" ]); } $dataList = DB::table('merchant_tax')->where('mid',$request->id)->get(); return response()->json([ 'rc' => 0, 'rm' => "sukses", 'list' => $dataList ]); break; case 'addTax': if (Auth::user()->role == 3) { return response()->json([ 'rc' => 99, 'rm' => "No Access" ]); } $listAdd = $request->tax_select; DB::table('merchant_tax')->where('mid', $request->get_mid_tax)->delete(); if ($listAdd) { foreach ($listAdd as $k => $v) { $datas = [ 'mid' => $request->get_mid_tax, 'tax_id' => $v ]; DB::table('merchant_tax')->insert($datas); $cGlobal->auditTrailValue("Merchant Tax",$titleTrail,'merchant_tax',json_encode($datas),''); } } break; } DB::commit(); return response()->json([ 'rc' => 0, 'rm' => "sukses" ]); } catch (QueryException $e){ if($e->getCode() == '23505'){ $response = "Terjadi Duplikasi Data, Data Gagal Disimpan !"; }else{ $response = "Terjadi Kesalahan, Data Tidak Sesuai !"; } DB::rollback(); return response()->json([ 'rc' => 99, 'rm' => $response, // 'msg' => $e->getMessage() ]); } } }