public function uploadW9(Request $request, $link, $app_name, $aff_code)
 {
     $affiliate_row = Affiliate::where('external_link', '=', $link)->first();
     if ($affiliate_row == null) {
         return redirect('error');
     }
     //$user_infusionsoft = UserInfusionsoft::where('app_name','=', $app_name)->first();
     $user_infusionsoft = $affiliate_row->infusionsoft_user;
     $app_infusionsoft = new iSDK();
     if (!$app_infusionsoft->cfgCon($user_infusionsoft->app_name, $user_infusionsoft->app_apikey)) {
         return redirect('error');
     }
     $file = $request->file('w9file');
     if ($file) {
         $extension = $file->getClientOriginalExtension();
         $mimeType = $file->getMimeType();
         if (strtolower($extension) != 'pdf' || $mimeType != 'application/pdf') {
             return redirect(URL::to('aff/' . $link . '/' . $app_name . '/' . $aff_code))->with('error', 'You can upload pdf files only.');
         }
         Storage::disk('local')->put($file->getFilename() . '.' . $extension, File::get($file));
         $affiliate_row->w9_file_original_name = $file->getClientOriginalName();
         $affiliate_row->w9_file = $file->getFilename() . '.' . $extension;
         $affiliate_row->save();
         return redirect(URL::to('aff/' . $link . '/' . $app_name . '/' . $aff_code))->with('success', 'Congratulations! Your W9 is uploaded.');
     }
     return redirect(URL::to('aff/' . $link . '/' . $app_name . '/' . $aff_code))->with('error', 'No file is selected.');
 }
 public function run()
 {
     DB::table('payments')->delete();
     //$user = \App\User::where('email','=','*****@*****.**')->first();
     $affiliate = \App\Affiliate::where('id', '>', 0)->first();
     \App\Payment::create(['affiliate_id' => $affiliate->id, 'payment_key' => 'AB029309', 'amount' => 1.0, 'pay_result' => '{}', 'ipn_result' => '', 'status' => 1]);
 }
 /**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     $crs = Course::find($id);
     $aff = Affiliate::all();
     $stm = Stream::all();
     $lvl = Level::all();
     return view('admin/edit_course')->with('course', $crs)->with('affiliates', $aff)->with('streams', $stm)->with('levels', $lvl);
 }
 public function run()
 {
     DB::table('affiliates')->delete();
     $user = \App\User::where('email', '=', '*****@*****.**')->first();
     $user_infusionsoft = \App\UserInfusionsoft::where('user_id', '=', $user->id)->first();
     \App\Affiliate::create(['user_is_id' => $user_infusionsoft->id, 'first_name' => 'Alex', 'last_name' => 'Santa', 'email' => '*****@*****.**', 'phone' => '00', 'paypal_email' => '*****@*****.**', 'password' => '', 'aff_code' => 'santa', 'aff_id' => 2, 'aff_program' => 2, 'send_confirmation' => false, 'confirm_w9' => false, 'send_monthly_stat' => false]);
     \App\Affiliate::create(['user_is_id' => $user_infusionsoft->id, 'first_name' => 'Mikael', 'last_name' => 'Laine', 'email' => '*****@*****.**', 'phone' => '01', 'paypal_email' => '*****@*****.**', 'password' => '', 'aff_code' => 'afafaf', 'aff_id' => 6, 'parent_aff_id' => 2, 'aff_program' => 2, 'send_confirmation' => false, 'confirm_w9' => false, 'send_monthly_stat' => false]);
     \App\Affiliate::create(['user_is_id' => $user_infusionsoft->id, 'first_name' => 'Test', 'last_name' => 'User', 'email' => '*****@*****.**', 'phone' => '03', 'paypal_email' => '', 'password' => '', 'aff_code' => 'atest', 'aff_id' => 4, 'parent_aff_id' => 2, 'aff_program' => 2, 'send_confirmation' => false, 'confirm_w9' => false, 'send_monthly_stat' => false]);
 }
 /**
  * Show the affiliate dashboard to the user.
  *
  * @return Response
  */
 public function downloadW9(Request $request, $affiliate_id)
 {
     $affiliate_row = Affiliate::where('id', '=', $affiliate_id)->first();
     if ($affiliate_row == null) {
         abort(404, "No affiliate");
     }
     if (!$affiliate_row->w9_file) {
         abort(404, "No W9 file.");
     }
     $file = Storage::disk('local')->get($affiliate_row->w9_file);
     $headers = ['Content-Type' => 'application/octet-stream', 'Content-Disposition: attachment; filename=' => $affiliate_row->w9_file_original_name];
     return new Response($file, 200, $headers);
 }
 public function postAddtag(Request $request, $affiliate_id)
 {
     $user_is = $request->user_infusionsoft;
     $app = $request->app_infusionsoft;
     $affiliate_row = Affiliate::where('user_is_id', '=', $user_is->id)->where('aff_id', '=', $affiliate_id)->first();
     $tag_id = $request->tag_id;
     $tag_name = $request->tag_name;
     $result = false;
     if ($tag_id !== '') {
         $result = $app->grpAssign($affiliate_row->contact_id, $tag_id);
     } else {
         if ($tag_name !== '') {
             $tagData = array('GroupName' => $tag_name);
             $tag_id = $app->dsAdd("ContactGroup", $tagData);
             $result = $app->grpAssign($affiliate_row->contact_id, $tag_id);
         }
     }
     $message = $result ? 'Tag has been applied successfully to the affiliate.' : 'Tag is not applied to the affiliate.';
     return redirect('/affiliates/' . $affiliate_id)->with('tag_message', $message);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     try {
         $aff = Affiliate::find($id);
         $aff->delete();
         \Session::flash('sucess_message', 'Sucessfully Deleted');
     } catch (\Exception $e) {
         \Session::flash('error_message', 'Affiliates already deleted!');
     }
     return redirect('myAdmin/affiliates');
 }
 /**
  * Show the application dashboard to the user.
  *
  * @return Response
  */
 public function index(Request $request)
 {
     $user_is = $request->user_infusionsoft;
     $app = $request->app_infusionsoft;
     $all_invoices = array();
     $returnFields = array('Id', 'AffiliateId', 'InvoiceTotal', 'TotalPaid', 'PayStatus', 'InvoiceTotal', 'Description');
     $query = array('Id' => '%');
     $page = 0;
     while (true) {
         $results = $app->dsQuery("Invoice", 1000, $page, $query, $returnFields);
         $all_invoices = array_merge($all_invoices, $results);
         if (count($results) < 1000) {
             break;
         }
         $page++;
     }
     $all_affiliates = array();
     $page = 0;
     $aff_fields = array('Id', 'AffCode', 'AffName', 'ContactId', 'ParentId', 'PayoutType', 'SalePercent', 'Status');
     $query = array('Id' => '%');
     while (true) {
         $results = $app->dsQuery("Affiliate", 1000, $page, $query, $aff_fields);
         $all_affiliates = array_merge($all_affiliates, $results);
         if (count($results) < 1000) {
             break;
         }
         $page++;
     }
     $affiliates_count = count($all_affiliates);
     $total_sales = 0;
     foreach ($all_invoices as $invoice) {
         if ($invoice['PayStatus']) {
             $total_sales += $invoice['TotalPaid'];
         }
     }
     $total_commission = 0;
     $total_payout = 0;
     $start_date = $request->start_date;
     if ($start_date == NULL) {
         $start_date = '01/01/2015';
     }
     $finish_date = $request->finish_date;
     if ($finish_date == NULL) {
         $finish_date = date('m/d/Y');
     }
     $start = $app->infuDate($start_date);
     $finish = $app->infuDate($finish_date);
     foreach ($all_affiliates as &$affiliate) {
         $affiliate['commission'] = 0;
         $affiliate['payout'] = 0;
         $affiliate_id = $affiliate['Id'];
         $commissions = $app->affCommissions($affiliate_id, $start, $finish);
         foreach ($commissions as $commission) {
             $affiliate['commission'] += $commission['AmtEarned'];
         }
         $total_commission += $affiliate['commission'];
         $payouts = $app->affPayouts($affiliate_id, $start, $finish);
         foreach ($payouts as &$payout) {
             $affiliate['payout'] += $payout['PayAmt'];
         }
         $affiliate_row = Affiliate::where('user_is_id', '=', $user_is->id)->where('aff_id', '=', $affiliate_id)->first();
         $payment_sum = DB::table('payments')->select(DB::raw('sum(amount) as amount'))->where('affiliate_id', '=', $affiliate_row->id)->where('created_at', '>=', date("Y-m-d H:i:s", strtotime($start_date)))->where('created_at', '<=', date("Y-m-d H:i:s", strtotime($finish_date)))->where('status', '=', 1)->first();
         $payout = 0;
         if ($payment_sum) {
             $payout = $payment_sum->amount;
         }
         $affiliate['payout'] += $payout;
         $total_payout += $payout;
         $affiliate['balance'] = $affiliate['commission'] - $affiliate['payout'];
         $affiliate['row'] = $affiliate_row;
     }
     $total_owed = $total_commission - $total_payout;
     return view('pages.home', compact('total_sales', 'total_commission', 'start_date', 'finish_date', 'total_owed', 'affiliates_count', 'all_affiliates'));
 }