public function add_pixel(Request $request)
 {
     if (Auth::check()) {
         if (in_array('ADD_EDIT_CREATIVE', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($validate->passes()) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                 }
                 if ($advertiser_obj) {
                     $active = 'Inactive';
                     if ($request->input('active') == 'on') {
                         $active = 'Active';
                     }
                     $rndstr = new AuditsController();
                     $pixel = new Pixel();
                     $pixel->name = $request->input('name');
                     $pixel->status = $active;
                     $pixel->advertiser_id = $request->input('advertiser_id');
                     $pixel->description = $request->input('description');
                     $pixel->version = 'version1';
                     $pixel->part_a = $rndstr->randomStr();
                     $pixel->part_b = $rndstr->randomStr();
                     $pixel->save();
                     $audit = new AuditsController();
                     $audit->store('pixel', $pixel->id, null, 'add');
                     return Redirect::to(url('/client/cl' . $advertiser_obj->GetClientID->id . '/advertiser/adv' . $request->input('advertiser_id') . '/pixel/pxl' . $pixel->id . '/edit'))->withErrors(['success' => true, 'msg' => "Pixel added successfully"]);
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => "Select An Advertiser first"]);
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => $validate->messages()->all()])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function add_offer(Request $request)
 {
     if (Auth::check()) {
         if (in_array('ADD_EDIT_OFFER', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($validate->passes()) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                 }
                 if ($advertiser_obj) {
                     $active = 'Inactive';
                     if ($request->input('active') == 'on') {
                         $active = 'Active';
                     }
                     $offer = new Offer();
                     $offer->name = $request->input('name');
                     $offer->advertiser_id = $request->input('advertiser_id');
                     $offer->description = $request->input('description');
                     $offer->status = $active;
                     $offer->save();
                     $audit = new AuditsController();
                     $audit->store('offer', $offer->id, null, 'add');
                     return Redirect::to(url('/client/cl' . $advertiser_obj->GetClientID->id . '/advertiser/adv' . $request->input('advertiser_id') . '/offer/ofr' . $offer->id . '/edit'))->withErrors(['success' => true, 'msg' => "Offer added successfully"]);
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => $validate->messages()->all()])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function UploadCampaign(Request $request)
 {
     if (Auth::check()) {
         if (in_array('ADD_EDIT_CAMPAIGN', $this->permission)) {
             if ($request->hasFile('upload')) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                     if (!$advertiser_obj) {
                         return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
                     }
                 }
                 if ($advertiser_obj) {
                     $destpath = public_path();
                     $extension = $request->file('upload')->getClientOriginalExtension();
                     // getting image extension
                     $fileName = str_random(32) . '.' . $extension;
                     $request->file('upload')->move($destpath . '/cdn/test/', $fileName);
                     Config::set('excel.import.startRow', 12);
                     $upload = Excel::load('public/cdn/test/' . $fileName, function ($reader) {
                     })->get();
                     $t = array();
                     foreach ($upload[0] as $key => $value) {
                         array_push($t, $key);
                     }
                     if ($t[1] != 'name' or $t[2] != 'max_impression' or $t[3] != 'daily_max_impression' or $t[4] != 'max_budget' or $t[5] != 'daily_max_budget' or $t[6] != 'cpm' or $t[7] != 'advertiser_domain_name' or $t[8] != 'status' or $t[9] != 'start_date' or $t[10] != 'end_date') {
                         File::delete($destpath . '/cdn/test/' . $fileName);
                         return Redirect::back()->withErrors(['success' => false, 'msg' => 'please be sure that file is correct'])->withInput();
                     }
                     $bad_input = array();
                     $count = 0;
                     //                        return dd();
                     foreach ($upload as $test) {
                         $flg = 0;
                         $campaign = new Campaign();
                         if ($test['name'] == '' or $test['max_impression'] == '' or $test['daily_max_impression'] == '' or $test['max_budget'] == '' or $test['daily_max_budget'] == '' or $test['cpm'] == '' or $test['advertiser_domain_name'] == '' or $test['status'] == '' or $test['status'] == '' or $test['end_date'] == '') {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         if (!is_numeric($test['max_impression']) or !is_numeric($test['daily_max_impression']) or !is_numeric($test['max_budget']) or !is_numeric($test['daily_max_budget']) or !is_numeric($test['cpm'])) {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         if (strcasecmp($test['status'], 'active') != 0 and strcasecmp($test['status'], 'inactive') != 0) {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         $campaign->name = $test['name'];
                         $campaign->max_impression = $test['max_impression'];
                         $campaign->daily_max_impression = $test['daily_max_impression'];
                         $campaign->max_budget = $test['max_budget'];
                         $campaign->daily_max_budget = $test['daily_max_budget'];
                         $campaign->cpm = $test['cpm'];
                         $campaign->start_date = $test['start_date'];
                         $campaign->end_date = $test['end_date'];
                         $campaign->advertiser_domain_name = $test['advertiser_domain_name'];
                         $campaign->status = ucwords(strtolower($test['status']));
                         $campaign->advertiser_id = $request->input('advertiser_id');
                         //                            return dd('dd1');
                         $count++;
                         $campaign->save();
                     }
                     $audit = new AuditsController();
                     $audit->store('campaign', 0, $count, 'bulk_add');
                     $msg = "Campaigns added successfully";
                     if (count($bad_input) > 0) {
                         $msg .= " exept: ";
                         foreach ($bad_input as $index) {
                             $msg .= $index . ',';
                         }
                     }
                     return Redirect::back()->withErrors(['success' => true, 'msg' => $msg]);
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select Advertiser First'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select a file'])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function jqgrid(Request $request)
 {
     if (Auth::check()) {
         if (in_array('ADD_EDIT_ADVERTISER', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($validate->passes()) {
                 $adver_id = substr($request->input('id'), 3);
                 if (User::isSuperAdmin()) {
                     $adver = Advertiser::find($adver_id);
                 } else {
                     $usr_company = $this->user_company();
                     $adver = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->with('Campaign')->with('Model')->with('GeoSegment')->with('BWList')->with('Creative')->with('GetClientID')->find($adver_id);
                 }
                 if ($adver) {
                     $data = array();
                     $audit = new AuditsController();
                     if ($adver->name != $request->input('name')) {
                         array_push($data, 'Name');
                         array_push($data, $adver->name);
                         array_push($data, $request->input('name'));
                         $adver->name = $request->input('name');
                     }
                     $audit->store('advertiser', $adver_id, $data, 'edit');
                     $adver->save();
                     return $msg = ['success' => true, 'msg' => "your Advertiser name Saved successfully"];
                 }
                 return $msg = ['success' => false, 'msg' => "Please Select an Advertiser First"];
             }
             return $msg = ['success' => false, 'msg' => "Please fill all Fields"];
         }
         return $msg = ['success' => false, 'msg' => "You don't have permission"];
     }
     return Redirect::to(url('user/login'));
 }
 public function add_geosegmentlist(Request $request)
 {
     //        return dd($request->all());
     if (Auth::check()) {
         if (in_array('ADD_EDIT_GEOSEGMENTLIST', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($validate->passes()) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                 }
                 if ($advertiser_obj) {
                     $chk = GeoSegmentList::where('advertiser_id', $request->input('advertiser_id'))->get();
                     //                        return dd($chk);
                     $flg = 0;
                     foreach ($chk as $index) {
                         if ($index->name == $request->input('name')) {
                             $flg = 1;
                         }
                     }
                     if ($flg == 0) {
                         $active = 'Inactive';
                         if ($request->input('active') == 'on') {
                             $active = 'Active';
                         }
                         $key = new AuditsController();
                         $key = $key->generateRandomString();
                         $audit = new AuditsController();
                         $geosegmentlist = new GeoSegmentList();
                         $geosegmentlist->name = $request->input('name');
                         $geosegmentlist->status = $active;
                         $geosegmentlist->advertiser_id = $request->input('advertiser_id');
                         $geosegmentlist->save();
                         $audit->store('geosegment', $geosegmentlist->id, null, 'add', $key);
                         for ($i = 0; $i < 5; $i++) {
                             if (!is_null($request->input('name' . $i)) and $request->input('name' . $i) != "") {
                                 $geosegment = new GeoSegment();
                                 $geosegment->name = $request->input('name' . $i);
                                 $geosegment->lat = $request->input('lat' . $i);
                                 $geosegment->lon = $request->input('lon' . $i);
                                 $geosegment->segment_radius = $request->input('segment_radius' . $i);
                                 $geosegment->geosegmentlist_id = $geosegmentlist->id;
                                 $geosegment->save();
                                 $audit->store('geosegmententrie', $geosegment->id, null, 'add', $key);
                             }
                         }
                         return Redirect::to(url('/client/cl' . $advertiser_obj->GetClientID->id . '/advertiser/adv' . $request->input('advertiser_id') . '/geosegment/gsm' . $geosegmentlist->id . '/edit'))->withErrors(['success' => true, 'msg' => "Geo Segmnet List added successfully"]);
                     }
                     return Redirect::back()->withErrors(['success' => false, 'msg' => 'this name already existed !!!'])->withInput();
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => $validate->messages()->all()])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function SubAudit($audit)
 {
     $audit_obj = array();
     foreach ($audit as $index) {
         $entity_obj = null;
         switch ($index->entity_type) {
             case 'user':
                 if (in_array('VIEW_USER', $this->permission)) {
                     $entity_obj = User::find($index->entity_id);
                 }
                 break;
             case 'company':
                 if (User::isSuperAdmin()) {
                     $entity_obj = \App\Models\Company::find($index->entity_id);
                 }
                 break;
             case 'inventory':
                 if (User::isSuperAdmin()) {
                     $entity_obj = Inventory::find($index->entity_id);
                 }
                 break;
             case 'client':
                 if (in_array('VIEW_CLIENT', $this->permission)) {
                     $entity_obj = Client::find($index->entity_id);
                 }
                 break;
             case 'advertiser':
                 if (in_array('VIEW_ADVERTISER', $this->permission)) {
                     $entity_obj = Advertiser::with('GetClientID')->find($index->entity_id);
                 }
                 break;
             case 'creative':
                 if (in_array('VIEW_CREATIVE', $this->permission)) {
                     $entity_obj = Creative::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'campaign':
                 if (in_array('VIEW_CAMPAIGN', $this->permission)) {
                     $entity_obj = Campaign::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'offer':
                 if (in_array('VIEW_OFFER', $this->permission)) {
                     $entity_obj = Offer::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'pixel':
                 if (in_array('VIEW_PIXEL', $this->permission)) {
                     $entity_obj = Pixel::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'targetgroup':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     $entity_obj = Targetgroup::find($index->entity_id);
                 }
                 break;
             case 'targetgroup_geolocation_map':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = Geolocation::find($index->after_value);
                     } else {
                         $entity_obj = Geolocation::find($index->entity_id);
                     }
                 }
                 break;
             case 'targetgroup_creative_map':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = Creative::find($index->after_value);
                     } else {
                         $entity_obj = Creative::find($index->entity_id);
                     }
                 }
                 break;
             case 'targetgroup_segment_map':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = Segment::find($index->after_value);
                     } else {
                         $entity_obj = Segment::find($index->entity_id);
                     }
                 }
                 break;
             case 'targetgroup_geosegment_map':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = GeoSegmentList::find($index->after_value);
                     } else {
                         $entity_obj = GeoSegmentList::find($index->entity_id);
                     }
                 }
                 break;
             case 'targetgroup_bwlist_map':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = BWList::find($index->after_value);
                     } else {
                         $entity_obj = BWList::find($index->entity_id);
                     }
                 }
                 break;
             case 'targetgroup_bidprofile_map':
                 if (in_array('VIEW_TARGETGROUP', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = Bid_Profile::find($index->after_value);
                     } else {
                         $entity_obj = Bid_Profile::find($index->entity_id);
                     }
                 }
                 break;
             case 'geosegment':
                 if (in_array('VIEW_GEOSEGMENTLIST', $this->permission)) {
                     $entity_obj = GeoSegmentList::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'geosegmententrie':
                 if (in_array('VIEW_GEOSEGMENTLIST', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = GeoSegment::find($index->after_value);
                     } else {
                         $entity_obj = GeoSegment::find($index->entity_id);
                     }
                 }
                 break;
             case 'bwlist':
                 if (in_array('VIEW_BWLIST', $this->permission)) {
                     $entity_obj = BWList::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'bwlistentrie':
                 if (in_array('VIEW_BWLIST', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = BWEntries::find($index->after_value);
                     } else {
                         $entity_obj = BWEntries::find($index->entity_id);
                     }
                 }
                 break;
             case 'bid_profile':
                 if (in_array('VIEW_BIDPROFILE', $this->permission)) {
                     $entity_obj = Bid_Profile::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'bid_profile_entry':
                 if (in_array('VIEW_BIDPROFILE', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = Bid_Profile::where('id', $index->after_value)->first();
                     } else {
                         $entity_obj = Bid_Profile_Entry::with('getParent')->find($index->entity_id);
                     }
                 }
                 break;
             case 'modelTable':
                 if (in_array('VIEW_MODEL', $this->permission)) {
                     if ($index->audit_type == 'del') {
                         $entity_obj = BWList::where('id', $index->after_value)->first();
                     } else {
                         $entity_obj = ModelTable::with(['getAdvertiser' => function ($q) {
                             $q->with('GetClientID');
                         }])->find($index->entity_id);
                     }
                 }
                 break;
             case 'offer_pixel_map':
                 if (in_array('VIEW_OFFER', $this->permission)) {
                     $entity_obj = Pixel::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'advertiser_model_map':
                 if (in_array('VIEW_ADVERTISER', $this->permission)) {
                     $entity_obj = ModelTable::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'positive_offer_model':
                 if (in_array('VIEW_MODEL', $this->permission)) {
                     $entity_obj = Offer::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
             case 'negative_offer_model':
                 if (in_array('VIEW_MODEL', $this->permission)) {
                     $entity_obj = Offer::with(['getAdvertiser' => function ($q) {
                         $q->with('GetClientID');
                     }])->find($index->entity_id);
                 }
                 break;
         }
         if (!is_null($entity_obj)) {
             array_push($audit_obj, $index);
             array_push($audit_obj, $entity_obj);
         }
     }
     return $audit_obj;
 }
 public function add_bwlist(Request $request)
 {
     if (Auth::check()) {
         if (in_array('ADD_EDIT_BWLIST', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($validate->passes()) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                 }
                 if ($advertiser_obj) {
                     $chk = BWList::where('advertiser_id', $request->input('advertiser_id'))->get();
                     //                        return dd($chk);
                     $flg = 0;
                     foreach ($chk as $index) {
                         if ($index->name == $request->input('name') and $index->list_type == $request->input('list_type')) {
                             $flg = 1;
                         }
                     }
                     if ($flg == 0) {
                         $active = 'Inactive';
                         if ($request->input('active') == 'on') {
                             $active = 'Active';
                         }
                         $key = new AuditsController();
                         $key = $key->generateRandomString();
                         $audit = new AuditsController();
                         $bwlist = new BWList();
                         $bwlist->name = $request->input('name');
                         $bwlist->status = $active;
                         $bwlist->list_type = $request->input('list_type');
                         $bwlist->advertiser_id = $request->input('advertiser_id');
                         $bwlist->save();
                         $audit->store('bwlist', $bwlist->id, null, 'add', $key);
                         $entries = explode(',', $request->input('domain_name'));
                         foreach ($entries as $index) {
                             $bwlistentries = new BWEntries();
                             $bwlistentries->domain_name = $index;
                             $bwlistentries->bwlist_id = $bwlist->id;
                             $bwlistentries->save();
                             $audit->store('bwlistentrie', $bwlistentries->id, null, 'add', $key);
                         }
                         return Redirect::to(url('/client/cl' . $advertiser_obj->GetClientID->id . '/advertiser/adv' . $request->input('advertiser_id') . '/bwlist/bwl' . $bwlist->id . '/edit'))->withErrors(['success' => true, 'msg' => "B/W List added successfully"]);
                     }
                     return Redirect::back()->withErrors(['success' => false, 'msg' => 'this name already existed !!!'])->withInput();
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => $validate->messages()->all()])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function getAssign($adv_id)
 {
     if (Auth::check()) {
         if (User::isSuperAdmin()) {
             $adver_obj = Advertiser::with('Creative', 'GeoSegment', 'BWList')->find($adv_id);
         } else {
             $usr_company = $this->user_company();
             $adver_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                 $p->whereIn('user_id', $usr_company);
             })->with('Creative', 'GeoSegment', 'BWList')->find($adv_id);
             if (!$adver_obj) {
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
             }
         }
         if ($adver_obj) {
             $geolocation_obj = Geolocation::get();
             return view('bulk.assign')->with('geolocation_obj', $geolocation_obj)->with('adver_obj', $adver_obj);
         }
     }
 }
 public function ChangeState(Request $request)
 {
     //        return dd($request->all());
     if (Auth::check()) {
         if (in_array('VIEW_ADVERTISER', $this->permission)) {
             //TODO: set report permission
             $type = $request->input('type');
             $arr = array();
             array_push($arr, $type);
             $time = '';
             $client = '';
             $advertiser = '';
             $creative = '';
             $geosegment = '';
             $campaign = '';
             $targetgroup = '';
             if (User::isSuperAdmin()) {
                 $clientArry = Client::get(['id'])->toArray();
                 $advertiserArry = Advertiser::get(['id'])->toArray();
                 $query = "1 = 1 ";
                 if ($request->has('client')) {
                     $query .= " and impression.client_id=" . $request->input('client');
                 }
                 if ($request->has('advertiser')) {
                     $query .= " and impression.advertiser_id=" . $request->input('advertiser');
                 }
                 if ($request->has('creative')) {
                     $query .= " and impression.creative_id=" . $request->input('creative');
                 }
                 if ($request->has('geosegment')) {
                     $query .= " and impression.geosegment_id=" . $request->input('geosegment');
                 }
                 if ($request->has('campaign')) {
                     $query .= " and impression.campaign_id=" . $request->input('campaign');
                 }
                 if ($request->has('targetgroup')) {
                     $query .= " and impression.targetgroup_id=" . $request->input('targetgroup');
                 }
                 if ($request->input('report_type') == 'today') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 60 * 60 * 24) . "' and '" . date('Y-m-d H:i:s', time() + 60 * 30 * 10) . "'";
                     $interval = 300;
                     $query .= " and impression.created_at " . $time;
                 }
                 if ($request->input('report_type') == '10m') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 600) . "' and '" . date('Y-m-d H:i:s') . "'";
                     $query .= " and impression.created_at " . $time;
                     $interval = 10;
                 }
                 if ($request->input('report_type') == '1h') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 60 * 60) . "' and '" . date('Y-m-d H:i:s') . "'";
                     $query .= " and impression.created_at " . $time;
                     $interval = 30;
                 }
                 if ($request->input('report_type') == '3h') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 60 * 60 * 3) . "' and '" . date('Y-m-d H:i:s') . "'";
                     $query .= " and impression.created_at " . $time;
                     $interval = 60;
                 }
                 if ($request->input('report_type') == '6h') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 60 * 60 * 6) . "' and '" . date('Y-m-d H:i:s') . "'";
                     $query .= " and impression.created_at " . $time;
                     $interval = 120;
                 }
                 if ($request->input('report_type') == '1D') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 60 * 60 * 24) . "' and '" . date('Y-m-d H:i:s') . "'";
                     $query .= " and impression.created_at " . $time;
                     $interval = 300;
                 }
                 if ($request->input('report_type') == '1M') {
                     $time = "between '" . date('Y-m-d H:i:s', time() - 60 * 60 * 24 * 30) . "' and '" . date('Y-m-d H:i:s') . "'";
                     $query .= " and impression.created_at " . $time;
                     $interval = 60 * 60;
                 }
                 if ($request->input('report_type') == 'rang') {
                     //todo: 120 noghte
                     if ($this->report_date_validation($request->input('date_range'))) {
                         $date_range = explode('&', $request->input('date_range'));
                         $start_date = explode('T', $date_range[0]);
                         $end_date = explode('T', $date_range[1]);
                         $time = "between '" . $start_date[0] . ' ' . $start_date[1] . "' and '" . $end_date[0] . ' ' . $end_date[1] . "'";
                         $query .= " and impression.created_at " . $time;
                         $interval = 24 * 60 * 60;
                     }
                 }
                 switch ($type) {
                     case 'client':
                         $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         if ($request->input('advertiser') == '') {
                             $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         }
                         break;
                     case 'report_type':
                         $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         $campaign = DB::table('impression')->join('campaign', 'impression.campaign_id', '=', 'campaign.id')->select(DB::raw('count(impression.campaign_id) as imps, impression.campaign_id as id , campaign.name'))->whereRaw($query)->groupBy('impression.campaign_id')->orderBy('imps', 'DESC')->get();
                         $targetgroup = DB::table('impression')->join('targetgroup', 'impression.targetgroup_id', '=', 'targetgroup.id')->select(DB::raw('count(impression.targetgroup_id) as imps, impression.targetgroup_id as id , targetgroup.name'))->whereRaw($query)->groupBy('impression.targetgroup_id')->orderBy('imps', 'DESC')->get();
                         $creative = DB::table('impression')->join('creative', 'impression.creative_id', '=', 'creative.id')->select(DB::raw('count(impression.creative_id) as imps, impression.creative_id as id , creative.name'))->whereRaw($query)->groupBy('impression.creative_id')->orderBy('imps', 'DESC')->get();
                         $geosegment = DB::table('impression')->join('geosegmentlist', 'impression.geosegment_id', '=', 'geosegmentlist.id')->select(DB::raw('count(impression.geosegment_id) as imps, impression.geosegment_id as id , geosegmentlist.name'))->whereRaw($query)->groupBy('impression.geosegment_id')->orderBy('imps', 'DESC')->get();
                         break;
                     case 'campaign':
                         $campaign_client_advertiser = Campaign::with(['getAdvertiser' => function ($q) {
                             $q->with('GetClientID');
                         }])->find($request->input('campaign'));
                         if ($request->input('client') == '') {
                             $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->where('impression.client_id', $campaign_client_advertiser->getAdvertiser->GetClientID->id)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         }
                         if ($request->input('advertiser') == '') {
                             $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->where('impression.advertiser_id', $campaign_client_advertiser->getAdvertiser->id)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         }
                         $campaign = DB::table('impression')->join('campaign', 'impression.campaign_id', '=', 'campaign.id')->select(DB::raw('count(impression.campaign_id) as imps, impression.campaign_id as id , campaign.name'))->whereRaw($query)->groupBy('impression.campaign_id')->orderBy('imps', 'DESC')->get();
                         break;
                     case 'targetgroup':
                         $targetgroup_client_advertiser = Targetgroup::with(['getCampaign' => function ($q) {
                             $q->with(['getAdvertiser' => function ($p) {
                                 $p->with('GetClientID');
                             }]);
                         }])->find($request->input('targetgroup'));
                         if ($request->input('client') == '') {
                             $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->where('impression.client_id', $targetgroup_client_advertiser->getCampaign->getAdvertiser->GetClientID->id)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         }
                         if ($request->input('advertiser') == '') {
                             $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->where('impression.advertiser_id', $targetgroup_client_advertiser->getCampaign->getAdvertiser->id)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         }
                         if ($request->input('campaign') == '') {
                             $campaign = DB::table('impression')->join('campaign', 'impression.campaign_id', '=', 'campaign.id')->select(DB::raw('count(impression.campaign_id) as imps, impression.campaign_id as id , campaign.name'))->where('impression.campaign_id', $targetgroup_client_advertiser->getCampaign->id)->groupBy('impression.campaign_id')->orderBy('imps', 'DESC')->get();
                         }
                         $targetgroup = DB::table('impression')->join('targetgroup', 'impression.targetgroup_id', '=', 'targetgroup.id')->select(DB::raw('count(impression.targetgroup_id) as imps, impression.targetgroup_id as id , targetgroup.name'))->whereRaw($query)->groupBy('impression.targetgroup_id')->orderBy('imps', 'DESC')->get();
                         break;
                     case 'advertiser':
                         if ($request->input('client') == '') {
                             $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->where('impression.advertiser_id', $request->input('advertiser'))->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         }
                         $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         break;
                     case 'creative':
                         $creativ_client_advertiser = Creative::with(['getAdvertiser' => function ($q) {
                             $q->with('GetClientID');
                         }])->find($request->input('creative'));
                         if ($request->input('client') == '') {
                             $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->where('impression.client_id', $creativ_client_advertiser->getAdvertiser->GetClientID->id)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         }
                         if ($request->input('advertiser') == '') {
                             $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->where('impression.advertiser_id', $creativ_client_advertiser->getAdvertiser->id)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         }
                         $creative = DB::table('impression')->join('creative', 'impression.creative_id', '=', 'creative.id')->select(DB::raw('count(impression.creative_id) as imps, impression.creative_id as id , creative.name'))->whereRaw($query)->groupBy('impression.creative_id')->orderBy('imps', 'DESC')->get();
                         break;
                     case 'geosegment':
                         $geosegment_client_advertiser = GeoSegmentList::with(['getAdvertiser' => function ($q) {
                             $q->with('GetClientID');
                         }])->find($request->input('geosegment'));
                         //                            return dd($geosegment_client_advertiser->getAdvertiser);
                         if ($request->input('client') == '') {
                             $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->where('impression.client_id', $geosegment_client_advertiser->getAdvertiser->GetClientID->id)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         }
                         if ($request->input('advertiser') == '') {
                             $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->where('impression.advertiser_id', $geosegment_client_advertiser->getAdvertiser->id)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         }
                         $geosegment = DB::table('impression')->join('geosegmentlist', 'impression.geosegment_id', '=', 'geosegmentlist.id')->select(DB::raw('count(impression.geosegment_id) as imps, impression.geosegment_id as id , geosegmentlist.name'))->whereRaw($query)->groupBy('impression.geosegment_id')->orderBy('imps', 'DESC')->get();
                         break;
                     case 'client_unfilter':
                         $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereIn('impression.advertiser_id', $advertiserArry)->whereIn('impression.client_id', $clientArry)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                         $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereIn('impression.advertiser_id', $advertiserArry)->whereIn('impression.client_id', $clientArry)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                         $campaign = DB::table('impression')->join('campaign', 'impression.campaign_id', '=', 'campaign.id')->select(DB::raw('count(impression.campaign_id) as imps, impression.campaign_id as id , campaign.name'))->whereIn('impression.advertiser_id', $advertiserArry)->whereIn('impression.client_id', $clientArry)->groupBy('impression.campaign_id')->orderBy('imps', 'DESC')->get();
                         $targetgroup = DB::table('impression')->join('targetgroup', 'impression.targetgroup_id', '=', 'targetgroup.id')->select(DB::raw('count(impression.targetgroup_id) as imps, impression.targetgroup_id as id , targetgroup.name'))->whereIn('impression.advertiser_id', $advertiserArry)->whereIn('impression.client_id', $clientArry)->groupBy('impression.targetgroup_id')->orderBy('imps', 'DESC')->get();
                         $creative = DB::table('impression')->join('creative', 'impression.creative_id', '=', 'creative.id')->select(DB::raw('count(impression.creative_id) as imps, impression.creative_id as id , creative.name'))->whereIn('impression.advertiser_id', $advertiserArry)->whereIn('impression.client_id', $clientArry)->groupBy('impression.creative_id')->orderBy('imps', 'DESC')->get();
                         $geosegment = DB::table('impression')->join('geosegmentlist', 'impression.geosegment_id', '=', 'geosegmentlist.id')->select(DB::raw('count(impression.geosegment_id) as imps, impression.geosegment_id as id , geosegmentlist.name'))->whereIn('impression.advertiser_id', $advertiserArry)->whereIn('impression.client_id', $clientArry)->groupBy('impression.geosegment_id')->orderBy('imps', 'DESC')->get();
                         break;
                 }
                 if ($request->input('client') == '' and $client == '') {
                     $client = DB::table('impression')->join('client', 'impression.client_id', '=', 'client.id')->select(DB::raw('count(impression.client_id) as imps, impression.client_id as id , client.name'))->whereRaw($query)->groupBy('impression.client_id')->orderBy('imps', 'DESC')->get();
                 }
                 if ($request->input('advertiser') == '' and $advertiser == '') {
                     $advertiser = DB::table('impression')->join('advertiser', 'impression.advertiser_id', '=', 'advertiser.id')->select(DB::raw('count(impression.advertiser_id) as imps, impression.advertiser_id as id , advertiser.name'))->whereRaw($query)->groupBy('impression.advertiser_id')->orderBy('imps', 'DESC')->get();
                 }
                 if ($request->input('campaign') == '' and $campaign == '') {
                     $campaign = DB::table('impression')->join('campaign', 'impression.campaign_id', '=', 'campaign.id')->select(DB::raw('count(impression.campaign_id) as imps, impression.campaign_id as id , campaign.name'))->whereRaw($query)->groupBy('impression.campaign_id')->orderBy('imps', 'DESC')->get();
                 }
                 if ($request->input('targetgroup') == '') {
                     $targetgroup = DB::table('impression')->join('targetgroup', 'impression.targetgroup_id', '=', 'targetgroup.id')->select(DB::raw('count(impression.targetgroup_id) as imps, impression.targetgroup_id as id , targetgroup.name'))->whereRaw($query)->groupBy('impression.targetgroup_id')->orderBy('imps', 'DESC')->get();
                 }
                 if ($request->input('creative') == '') {
                     $creative = DB::table('impression')->join('creative', 'impression.creative_id', '=', 'creative.id')->select(DB::raw('count(impression.creative_id) as imps, impression.creative_id as id , creative.name'))->whereRaw($query)->groupBy('impression.creative_id')->orderBy('imps', 'DESC')->get();
                 }
                 if ($request->input('geosegmentlist') == '') {
                     $geosegment = DB::table('impression')->join('geosegmentlist', 'impression.geosegment_id', '=', 'geosegmentlist.id')->select(DB::raw('count(impression.geosegment_id) as imps, impression.geosegment_id as id , geosegmentlist.name'))->whereRaw($query)->groupBy('impression.geosegment_id')->orderBy('imps', 'DESC')->get();
                 }
                 //                    return dd($query);
                 if ($time != '') {
                     $impChart = Impression::where('event_type', 'impression')->whereRaw($query)->whereRaw('created_at ' . $time)->orderBy('created_at', 'ASC')->get();
                     $imps = 1;
                     $flg = 0;
                     //                        return dd($impChart);
                     $impsString = "Date,Imps\n";
                     if ($impChart) {
                         foreach ($impChart as $key => $index) {
                             if ($flg == 0) {
                                 $timeToShow = $index->created_at;
                                 $firstHop = strtotime($index->created_at);
                                 $nextHop = $firstHop + $interval;
                                 $flg = 1;
                             }
                             if (isset($impChart[$key + 1]) and strtotime($impChart[$key + 1]->created_at) >= $nextHop) {
                                 $impsString .= $timeToShow . ",0;" . $imps . ";0\n";
                                 $flg = 0;
                                 $imps = 0;
                             }
                             if (!isset($impChart[$key + 1])) {
                             }
                             $imps++;
                         }
                     }
                     //                        return dd($impsString);
                     $clkChart = Impression::where('event_type', 'click')->whereRaw($query)->whereRaw('created_at ' . $time)->orderBy('created_at', 'ASC')->get();
                     $imps = 1;
                     $flg = 0;
                     $clkString = "Date,Clicks\n";
                     foreach ($clkChart as $key => $index) {
                         if ($flg == 0) {
                             $timeToShow = $index->created_at;
                             $firstHop = strtotime($index->created_at);
                             $nextHop = $firstHop + $interval;
                             $flg = 1;
                         }
                         if (isset($clkChart[$key + 1]) and strtotime($clkChart[$key + 1]->created_at) >= $nextHop) {
                             $clkString .= $timeToShow . ",0;" . $imps . ";0\n";
                             $flg = 0;
                             $imps = 0;
                         }
                         if (!isset($clkChart[$key + 1])) {
                         }
                         $imps++;
                     }
                     $cnvChart = Impression::where('event_type', 'conversion')->whereRaw($query)->whereRaw('created_at ' . $time)->orderBy('created_at', 'ASC')->get();
                     $imps = 1;
                     $flg = 0;
                     $cnvString = "Date,Conversion\n";
                     foreach ($cnvChart as $key => $index) {
                         if ($flg == 0) {
                             $timeToShow = $index->created_at;
                             $firstHop = strtotime($index->created_at);
                             $nextHop = $firstHop + $interval;
                             $flg = 1;
                         }
                         if (isset($cnvChart[$key + 1]) and strtotime($cnvChart[$key + 1]->created_at) >= $nextHop) {
                             $cnvString .= $timeToShow . ",0;" . $imps . ";0\n";
                             $flg = 0;
                             $imps = 0;
                         }
                         if (!isset($cnvChart[$key + 1])) {
                         }
                         $imps++;
                     }
                     array_push($arr, $client);
                     array_push($arr, $advertiser);
                     array_push($arr, $creative);
                     array_push($arr, $geosegment);
                     array_push($arr, $campaign);
                     array_push($arr, $targetgroup);
                     array_push($arr, $impsString);
                     array_push($arr, $clkString);
                     array_push($arr, $cnvString);
                 }
                 return json_encode($arr);
                 $clients = Client::get();
                 $advertiser = Advertiser::get();
                 //                    $advertiser = Advertiser::with(['Campaign' => function ($q) {
                 //                        $q->select(DB::raw('*,count(advertiser_id) as advertiser_count'))->groupBy('advertiser_id');
                 //                    }])->with('GetClientID')->get();
             } else {
                 $usr_company = User::where('company_id', Auth::user()->company_id)->get(['id'])->toArray();
                 $clients = Client::whereIn('user_id', $usr_company)->get();
                 $advertiser = Advertiser::with(['GetClientID' => function ($p) use($usr_company) {
                     $p->whereIn('user_id', $usr_company);
                 }])->get();
             }
             //                return dd($advertiser);
             return view('report.report')->with('clients', $clients)->with('advertiser', $advertiser);
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('user/login'));
 }
 public function add_model(Request $request)
 {
     //        return dd($request->all());
     if (Auth::check()) {
         if (in_array('ADD_EDIT_MODEL', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($validate->passes()) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                 }
                 if ($advertiser_obj) {
                     $audit = new AuditsController();
                     $audit_key = $audit->generateRandomString();
                     if ($request->has('positive_offer_id')) {
                         $positive_offer_id = implode(',', $request->input('positive_offer_id'));
                     }
                     if ($request->has('negative_offer_id')) {
                         $negative_offer_id = implode(',', $request->input('negative_offer_id'));
                     }
                     //                        return dd('['.$positive_offer_id.']');
                     $modelTable = new ModelTable();
                     $modelTable->name = $request->input('name');
                     $modelTable->advertiser_id = $request->input('advertiser_id');
                     $modelTable->seed_web_sites = $request->has('seed_web_sites') ? json_encode($request->input('seed_web_sites')) : '';
                     $modelTable->algo = $request->input('algo');
                     $modelTable->segment_name_seed = $request->input('segment_name_seed');
                     $modelTable->process_result = 'submitted';
                     $modelTable->description = $request->input('description');
                     $modelTable->feature_recency_in_sec = $request->input('feature_recency_in_sec');
                     $modelTable->max_num_both_neg_pos_devices = $request->input('max_num_both_neg_pos_devices');
                     $modelTable->negative_features_requested = $request->has('negative_features_requested') ? json_encode($request->input('negative_features_requested')) : '';
                     $modelTable->model_type = $request->input('model_type');
                     $modelTable->cut_off_score = $request->input('cut_off_score');
                     $modelTable->pixel_hit_recency_in_seconds = $request->input('pixel_hit_recency_in_seconds');
                     if ($request->has('positive_offer_id')) {
                         $modelTable->positive_offer_id = $positive_offer_id;
                     }
                     if ($request->has('negative_offer_id')) {
                         $modelTable->negative_offer_id = $negative_offer_id;
                     }
                     $modelTable->max_number_of_device_history_per_feature = $request->input('max_number_of_device_history_per_feature');
                     $modelTable->max_number_of_negative_feature_to_pick = $request->input('max_number_of_negative_feature_to_pick');
                     $modelTable->number_of_positive_device_to_be_used_for_modeling = $request->input('number_of_positive_device_to_be_used_for_modeling');
                     $modelTable->number_of_negative_device_to_be_used_for_modeling = $request->input('number_of_negative_device_to_be_used_for_modeling');
                     $modelTable->number_of_both_negative_positive_device_to_be_used = $request->input('number_of_both_negative_positive_device_to_be_used');
                     $modelTable->date_of_request_completion = date('Y-m-d H:i:s');
                     $modelTable->date_of_request = date('Y-m-d H:i:s');
                     $modelTable->save();
                     if ($request->has('positive_offer_id')) {
                         foreach ($request->input('positive_offer_id') as $index) {
                             $audit->store('positive_offer_model', $index, $modelTable->id, 'add', $audit_key);
                         }
                     }
                     if ($request->has('negative_offer_id')) {
                         foreach ($request->input('negative_offer_id') as $index) {
                             $audit->store('negative_offer_model', $index, $modelTable->id, 'add', $audit_key);
                         }
                     }
                     //                        return dd($request->all());
                     $audit->store('modelTable', $modelTable->id, null, 'add', $audit_key);
                     return Redirect::to(url('/client/cl' . $advertiser_obj->GetClientID->id . '/advertiser/adv' . $request->input('advertiser_id') . '/model/mdl' . $modelTable->id . '/edit'))->withErrors(['success' => true, 'msg' => "Model added successfully"]);
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => $validate->messages()->all()])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function UploadCreative(Request $request)
 {
     if (Auth::check()) {
         if (in_array('ADD_EDIT_CREATIVE', $this->permission)) {
             if ($request->hasFile('upload')) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                     if (!$advertiser_obj) {
                         return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
                     }
                 }
                 if ($advertiser_obj) {
                     $destpath = public_path();
                     $extension = $request->file('upload')->getClientOriginalExtension();
                     // getting image extension
                     $fileName = str_random(32) . '.' . $extension;
                     $request->file('upload')->move($destpath . '/cdn/test/', $fileName);
                     Config::set('excel.import.startRow', 12);
                     $upload = Excel::load('public/cdn/test/' . $fileName, function ($reader) {
                     })->get();
                     $t = array();
                     foreach ($upload[0] as $key => $value) {
                         array_push($t, $key);
                     }
                     if ($t[1] != 'name' or $t[2] != 'ad_tag' or $t[3] != 'landing_page_url' or $t[4] != 'preview_url' or $t[5] != 'size' or $t[6] != 'attributes' or $t[7] != 'advertiser_domain_name' or $t[8] != 'status' or $t[9] != 'api' or $t[10] != 'ad_type') {
                         File::delete($destpath . '/cdn/test/' . $fileName);
                         return Redirect::back()->withErrors(['success' => false, 'msg' => 'please be sure that file is correct'])->withInput();
                     }
                     $bad_input = array();
                     $count = 0;
                     foreach ($upload as $test) {
                         $flg = 0;
                         $creative = new Creative();
                         if ($test['name'] == '' or $test['ad_tag'] == '' or $test['landing_page_url'] == '' or $test['preview_url'] == '' or $test['size'] == '' or $test['attributes'] == '' or $test['advertiser_domain_name'] == '' or $test['status'] == '' or $test['api'] == '' or $test['ad_type'] == '') {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         if (strcasecmp($test['status'], 'active') != 0 and strcasecmp($test['status'], 'inactive') != 0) {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         $api = explode(',', $test['api']);
                         if (is_array($api)) {
                             foreach ($api as $index) {
                                 if (strcasecmp($index, 'VPAID_1.0') != 0 and strcasecmp($index, 'VPAID_2.0') != 0 and strcasecmp($index, 'MRAID-1') != 0 and strcasecmp($index, 'ORMMA') != 0 and strcasecmp($index, 'MRAID-2') != 0) {
                                     array_push($bad_input, $test['name']);
                                     $flg = 1;
                                 }
                             }
                             if ($flg == 1) {
                                 $flg = 0;
                                 continue;
                             }
                         } else {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         if (strcasecmp($test['ad_type'], 'IFRAME') != 0 and strcasecmp($test['ad_type'], 'JAVASCRIPT') != 0 and strcasecmp($test['ad_type'], 'XHTML_BANNER_AD') != 0 and strcasecmp($test['ad_type'], 'XHTML_TEXT_AD') != 0) {
                             array_push($bad_input, $test['name']);
                             return dd('ss2');
                             continue;
                         }
                         $size = explode('x', $test['size']);
                         if (!is_array($size) or count($size) != 2 or is_array($size) and count($size) == 2 and !is_numeric($size[0]) or is_array($size) and count($size) == 2 and !is_numeric($size[1])) {
                             array_push($bad_input, $test['name']);
                             continue;
                         }
                         $creative->name = $test['name'];
                         $creative->ad_tag = $test['ad_tag'];
                         $creative->landing_page_url = $test['landing_page_url'];
                         $creative->preview_url = $test['preview_url'];
                         $creative->size = $size[0] . 'x' . $size[1];
                         $creative->attributes = $test['attributes'];
                         $creative->advertiser_domain_name = $test['advertiser_domain_name'];
                         $creative->status = ucwords(strtolower($test['status']));
                         $creative->api = '[' . strtoupper(implode(',', $api)) . ']';
                         $creative->ad_type = strtoupper($test['ad_type']);
                         $creative->advertiser_id = $request->input('advertiser_id');
                         $creative->save();
                         $count++;
                     }
                     $audit = new AuditsController();
                     $audit->store('creative', 0, $count, 'bulk_add');
                     $msg = "Creatives added successfully";
                     if (count($bad_input) > 0) {
                         $msg .= " exept: ";
                         foreach ($bad_input as $index) {
                             $msg .= $index . ',';
                         }
                     }
                     return Redirect::back()->withErrors(['success' => true, 'msg' => $msg]);
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select Advertiser First'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select a file'])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }
 public function UploadBidProfile(Request $request)
 {
     //        return dd($request->all());
     if (Auth::check()) {
         if (in_array('ADD_EDIT_BIDPROFILE', $this->permission)) {
             $validate = \Validator::make($request->all(), ['name' => 'required']);
             if ($request->hasFile('upload_bid_profile') and $validate->passes()) {
                 if (User::isSuperAdmin()) {
                     $advertiser_obj = Advertiser::with('GetClientID')->find($request->input('advertiser_id'));
                 } else {
                     $usr_company = $this->user_company();
                     $advertiser_obj = Advertiser::whereHas('GetClientID', function ($p) use($usr_company) {
                         $p->whereIn('user_id', $usr_company);
                     })->find($request->input('advertiser_id'));
                     if (!$advertiser_obj) {
                         return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
                     }
                 }
                 if ($advertiser_obj) {
                     $destpath = public_path();
                     $extension = $request->file('upload_bid_profile')->getClientOriginalExtension();
                     // getting image extension
                     $fileName = str_random(32) . '.' . $extension;
                     $request->file('upload_bid_profile')->move($destpath . '/cdn/test/', $fileName);
                     $upload = Excel::load('public/cdn/test/' . $fileName, function ($reader) {
                     })->get();
                     //                        return dd($upload);
                     $t = array();
                     foreach ($upload[0] as $key => $value) {
                         array_push($t, $key);
                     }
                     if ($t[0] != 'domain' or $t[1] != 'bid_strategy' or $t[2] != 'bid_value') {
                         File::delete($destpath . '/cdn/test/' . $fileName);
                         return Redirect::back()->withErrors(['success' => false, 'msg' => 'please be sure that file is correct'])->withInput();
                     }
                     $chk = Bid_Profile::where('advertiser_id', $request->input('advertiser_id'))->get();
                     foreach ($chk as $index) {
                         if ($index->name == $request->input('name')) {
                             File::delete($destpath . '/cdn/test/' . $fileName);
                             return Redirect::back()->withErrors(['success' => false, 'msg' => 'this name already existed !!!'])->withInput();
                         }
                     }
                     $bid_profile = new Bid_Profile();
                     $bid_profile->name = $request->input('name');
                     $bid_profile->advertiser_id = $request->input('advertiser_id');
                     $bid_profile->status = 'Active';
                     $bid_profile->save();
                     foreach ($upload as $test) {
                         $flg = 0;
                         if ($test['bid_strategy'] == 'Absolute') {
                             if ($test['bid_value'] < 0 and $test['bid_value'] > 10) {
                                 $flg = 1;
                             }
                         } elseif ($test['bid_strategy'] == 'Percentage') {
                             if ($test['bid_value'] < 0 and $test['bid_value'] > 100) {
                                 $flg = 1;
                             }
                         } elseif (!preg_match($this->pattern, $test['domain'])) {
                             $flg = 1;
                         }
                         if ($flg == 0) {
                             $bid_profile_entry = new Bid_Profile_Entry();
                             $bid_profile_entry->domain = $test['domain'];
                             $bid_profile_entry->bid_strategy = $test['bid_strategy'];
                             $bid_profile_entry->bid_value = $test['bid_value'];
                             $bid_profile_entry->bid_profile_id = $bid_profile->id;
                             $bid_profile_entry->save();
                         }
                     }
                     $msg = "Bid Profile added successfully";
                     return Redirect::back()->withErrors(['success' => true, 'msg' => $msg]);
                 }
                 return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select your Client'])->withInput();
             }
             return Redirect::back()->withErrors(['success' => false, 'msg' => 'please Select a file or fill name '])->withInput();
         }
         return Redirect::back()->withErrors(['success' => false, 'msg' => "You don't have permission"]);
     }
     return Redirect::to(url('/user/login'));
 }