123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\BusinessSetting;
- use Artisan;
- use Illuminate\Http\Request;
- use App\Models\AffiliateOption;
- use App\Models\Order;
- use App\Models\AffiliateConfig;
- use App\Models\AffiliateUser;
- use App\Models\AffiliatePayment;
- use App\Models\AffiliateWithdrawRequest;
- use App\Models\AffiliateLog;
- use App\Models\AffiliateStats;
- use Carbon\Carbon;
- use App\Models\User;
- use App\Models\Customer;
- use App\Models\Category;
- use Auth;
- use DB;
- use Hash;
- use Illuminate\Auth\Events\Registered;
- use function collect;
- use function count;
- use function dd;
- use function get_setting;
- use function print_r;
- use function with;
- class AffiliateController extends Controller
- {
- //
- public function index() {
- return view('affiliate.index');
- }
- public function affiliate_option_store( Request $request ) {
- //dd($request->all());
- $affiliate_option = AffiliateOption::where('type', $request->type)->first();
- if ( $affiliate_option == NULL )
- {
- $affiliate_option = new AffiliateOption;
- }
- $affiliate_option->type = $request->type;
- $commision_details = [];
- if ( $request->type == 'user_registration_first_purchase' )
- {
- $affiliate_option->percentage = $request->percentage;
- }
- else if ( $request->type == 'product_sharing' )
- {
- $commision_details['commission'] = $request->amount;
- $commision_details['commission_type'] = $request->amount_type;
- }
- else if ( $request->type == 'category_wise_affiliate' )
- {
- foreach ( Category::all() as $category )
- {
- $data['category_id'] = $request['categories_id_' . $category->id];
- $data['commission'] = $request['commison_amounts_' . $category->id];
- $data['commission_type'] = $request['commison_types_' . $category->id];
- array_push($commision_details, $data);
- }
- }
- else if ( $request->type == 'max_affiliate_limit' )
- {
- $affiliate_option->percentage = $request->percentage;
- }
- $affiliate_option->details = json_encode($commision_details);
- if ( $request->has('status') )
- {
- $affiliate_option->status = 1;
- if ( $request->type == 'product_sharing' )
- {
- $affiliate_option_status_update = AffiliateOption::where('type', 'category_wise_affiliate')->first();
- $affiliate_option_status_update->status = 0;
- $affiliate_option_status_update->save();
- }
- if ( $request->type == 'category_wise_affiliate' )
- {
- $affiliate_option_status_update = AffiliateOption::where('type', 'product_sharing')->first();
- $affiliate_option_status_update->status = 0;
- $affiliate_option_status_update->save();
- }
- }
- else
- {
- $affiliate_option->status = 0;
- }
- $affiliate_option->save();
- flash("This has been updated successfully")->success();
- return back();
- }
- public function configs() {
- return view('affiliate.configs');
- }
- public function config_store( Request $request ) {
- if ( $request->type == 'validation_time' )
- {
- //affiliate validation time
- $affiliate_config = AffiliateConfig::where('type', $request->type)->first();
- if ( $affiliate_config == NULL )
- {
- $affiliate_config = new AffiliateConfig;
- }
- $affiliate_config->type = $request->type;
- $affiliate_config->value = $request[$request->type];
- $affiliate_config->save();
- flash("Validation time updated successfully")->success();
- }
- else
- {
- $form = [];
- $select_types = [ 'select', 'multi_select', 'radio' ];
- $j = 0;
- for ( $i = 0; $i < count($request->type); $i++ )
- {
- $item['type'] = $request->type[$i];
- $item['label'] = $request->label[$i];
- if ( in_array($request->type[$i], $select_types) )
- {
- $item['options'] = json_encode($request['options_' . $request->option[$j]]);
- $j++;
- }
- array_push($form, $item);
- }
- $affiliate_config = AffiliateConfig::where('type', 'verification_form')->first();
- $affiliate_config->value = json_encode($form);
- flash("Verification form updated successfully")->success();
- }
- if ( $affiliate_config->save() )
- {
- return back();
- }
- }
- public function apply_for_affiliate( Request $request ) {
- if ( Auth::check() && AffiliateUser::where('user_id', Auth::user()->id)->first() != NULL )
- {
- flash(translate("You are already an affiliate user!"))->warning();
- return back();
- }
- return view('affiliate.frontend.apply_for_affiliate');
- }
- public function affiliate_logs_admin() {
- $affiliate_logs = AffiliateLog::latest()->paginate(10);
- return view('affiliate.affiliate_logs', compact('affiliate_logs'));
- }
- public function store_affiliate_user( Request $request ) {
- if ( !Auth::check() )
- {
- if ( User::where('email', $request->email)->first() != NULL )
- {
- flash(translate('Email already exists!'))->error();
- return back();
- }
- if ( $request->password == $request->password_confirmation )
- {
- $user = new User;
- $user->name = $request->name;
- $user->email = $request->email;
- $user->user_type = "customer";
- $user->password = Hash::make($request->password);
- $user->save();
- $customer = new Customer;
- $customer->user_id = $user->id;
- $customer->save();
- auth()->login($user, false);
- if ( get_setting('email_verification') != 1 )
- {
- $user->email_verified_at = date('Y-m-d H:m:s');
- $user->save();
- }
- else
- {
- event(new Registered($user));
- }
- }
- else
- {
- flash(translate('Sorry! Password did not match.'))->error();
- return back();
- }
- }
- $affiliate_user = Auth::user()->affiliate_user;
- if ( $affiliate_user == NULL )
- {
- $affiliate_user = new AffiliateUser;
- $affiliate_user->user_id = Auth::user()->id;
- }
- $data = [];
- $i = 0;
- foreach ( json_decode(AffiliateConfig::where('type', 'verification_form')->first()->value) as $key => $element )
- {
- $item = [];
- if ( $element->type == 'text' )
- {
- $item['type'] = 'text';
- $item['label'] = $element->label;
- $item['value'] = $request['element_' . $i];
- }
- else if ( $element->type == 'select' || $element->type == 'radio' )
- {
- $item['type'] = 'select';
- $item['label'] = $element->label;
- $item['value'] = $request['element_' . $i];
- }
- else if ( $element->type == 'multi_select' )
- {
- $item['type'] = 'multi_select';
- $item['label'] = $element->label;
- $item['value'] = json_encode($request['element_' . $i]);
- }
- else if ( $element->type == 'file' )
- {
- $item['type'] = 'file';
- $item['label'] = $element->label;
- $item['value'] = $request['element_' . $i]->store('uploads/affiliate_verification_form');
- }
- array_push($data, $item);
- $i++;
- }
- $affiliate_user->informations = json_encode($data);
- if ( $affiliate_user->save() )
- {
- flash(translate('Your verification request has been submitted successfully!'))->success();
- return redirect()->route('home');
- }
- flash(translate('Sorry! Something went wrong.'))->error();
- return back();
- }
- public function users() {
- $affiliate_users = AffiliateUser::paginate(12);
- return view('affiliate.users', compact('affiliate_users'));
- }
- public function show_verification_request( $id ) {
- $affiliate_user = AffiliateUser::findOrFail($id);
- return view('affiliate.show_verification_request', compact('affiliate_user'));
- }
- public function approve_user( $id ) {
- $affiliate_user = AffiliateUser::findOrFail($id);
- $affiliate_user->status = 1;
- if ( $affiliate_user->save() )
- {
- flash(translate('Affiliate user has been approved successfully'))->success();
- return redirect()->route('affiliate.users');
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- public function reject_user( $id ) {
- $affiliate_user = AffiliateUser::findOrFail($id);
- $affiliate_user->status = 0;
- $affiliate_user->informations = NULL;
- if ( $affiliate_user->save() )
- {
- flash(translate('Affiliate user request has been rejected successfully'))->success();
- return redirect()->route('affiliate.users');
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- public function updateApproved( Request $request ) {
- $affiliate_user = AffiliateUser::findOrFail($request->id);
- $affiliate_user->status = $request->status;
- if ( $affiliate_user->save() )
- {
- return 1;
- }
- return 0;
- }
- public function payment_modal( Request $request ) {
- $affiliate_user = AffiliateUser::findOrFail($request->id);
- return view('affiliate.payment_modal', compact('affiliate_user'));
- }
- public function payment_store( Request $request ) {
- $affiliate_payment = new AffiliatePayment;
- $affiliate_payment->affiliate_user_id = $request->affiliate_user_id;
- $affiliate_payment->amount = $request->amount;
- $affiliate_payment->payment_method = $request->payment_method;
- $affiliate_payment->save();
- $affiliate_user = AffiliateUser::findOrFail($request->affiliate_user_id);
- $affiliate_user->balance -= $request->amount;
- $affiliate_user->save();
- flash(translate('Payment completed'))->success();
- return back();
- }
- public function payment_history( $id ) {
- $affiliate_user = AffiliateUser::findOrFail(decrypt($id));
- $affiliate_payments = $affiliate_user->affiliate_payments();
- return view('affiliate.payment_history', compact('affiliate_payments', 'affiliate_user'));
- }
- public function user_index( Request $request ) {
- $affiliate_logs = AffiliateLog::where('referred_by_user', Auth::user()->id)->latest()->paginate(10);
- $query = AffiliateStats::query();
- $query = $query->select(
- DB::raw('SUM(no_of_click) AS count_click, SUM(no_of_order_item) AS count_item, SUM(no_of_delivered) AS count_delivered, SUM(no_of_cancel) AS count_cancel')
- );
- if ( $request->type == 'Today' )
- {
- $query->whereDate('created_at', Carbon::today());
- }
- else if ( $request->type == '7' || $request->type == '30' )
- {
- $query->whereRaw('created_at <= NOW() AND created_at >= DATE_SUB(created_at, INTERVAL ' . $request->type . ' DAY)');
- }
- $query->where('affiliate_user_id', Auth::user()->id);
- $affliate_stats = $query->first();
- $type = $request->type;
- return view('affiliate.frontend.index', compact('affiliate_logs', 'affliate_stats', 'type'));
- }
- // payment history for user
- public function user_payment_history() {
- $affiliate_user = Auth::user()->affiliate_user;
- $affiliate_payments = $affiliate_user->affiliate_payments();
- return view('affiliate.frontend.payment_history', compact('affiliate_payments'));
- }
- // withdraw request history for user
- public function user_withdraw_request_history() {
- $affiliate_user = Auth::user()->affiliate_user;
- $affiliate_withdraw_requests = AffiliateWithdrawRequest::where('user_id', Auth::user()->id)->orderBy('id', 'desc')->paginate(10);
- return view('affiliate.frontend.withdraw_request_history', compact('affiliate_withdraw_requests'));
- }
- public function payment_settings() {
- $affiliate_user = Auth::user()->affiliate_user;
- return view('affiliate.frontend.payment_settings', compact('affiliate_user'));
- }
- public function payment_settings_store( Request $request ) {
- $affiliate_user = Auth::user()->affiliate_user;
- $affiliate_user->paypal_email = $request->paypal_email;
- $affiliate_user->bank_information = $request->bank_information;
- $affiliate_user->save();
- flash(translate('Affiliate payment settings has been updated successfully'))->success();
- return redirect()->route('affiliate.user.index');
- }
- public function processAffiliatePoints( Order $order ) {
- if ( addon_is_activated('affiliate_system') )
- {
- if ( AffiliateOption::where('type', 'user_registration_first_purchase')->first()->status )
- {
- if ( $order->user != NULL && $order->user->orders->count() == 1 )
- {
- if ( $order->user->referred_by != NULL )
- {
- $user = User::find($order->user->referred_by);
- if ( $user != NULL )
- {
- $amount = ( AffiliateOption::where('type', 'user_registration_first_purchase')->first()->percentage * $order->grand_total ) / 100;
- $affiliate_user = $user->affiliate_user;
- if ( $affiliate_user != NULL )
- {
- $affiliate_user->balance += $amount;
- $affiliate_user->save();
- // Affiliate log
- $affiliate_log = new AffiliateLog;
- $affiliate_log->user_id = $order->user_id;
- $affiliate_log->referred_by_user = $order->user->referred_by;
- $affiliate_log->amount = $amount;
- $affiliate_log->order_id = $order->id;
- $affiliate_log->affiliate_type = 'user_registration_first_purchase';
- $affiliate_log->save();
- }
- }
- }
- }
- }
- if ( AffiliateOption::where('type', 'product_sharing')->first()->status )
- {
- foreach ( $order->orderDetails as $key => $orderDetail )
- {
- $amount = 0;
- if ( $orderDetail->product_referral_code != NULL )
- {
- $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
- if ( $referred_by_user != NULL )
- {
- if ( AffiliateOption::where('type', 'product_sharing')->first()->details != NULL && json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission_type == 'amount' )
- {
- $amount = json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission;
- }
- else if ( AffiliateOption::where('type', 'product_sharing')->first()->details != NULL && json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission_type == 'percent' )
- {
- $amount = ( json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission * $orderDetail->price ) / 100;
- }
- $affiliate_user = $referred_by_user->affiliate_user;
- if ( $affiliate_user != NULL )
- {
- $affiliate_user->balance += $amount;
- $affiliate_user->save();
- // Affiliate log
- $affiliate_log = new AffiliateLog;
- if ( $order->user_id != NULL )
- {
- $affiliate_log->user_id = $order->user_id;
- }
- else
- {
- $affiliate_log->guest_id = $order->guest_id;
- }
- $affiliate_log->referred_by_user = $referred_by_user->id;
- $affiliate_log->amount = $amount;
- $affiliate_log->order_id = $order->id;
- $affiliate_log->order_detail_id = $orderDetail->id;
- $affiliate_log->affiliate_type = 'product_sharing';
- $affiliate_log->save();
- }
- }
- }
- }
- }
- else if ( AffiliateOption::where('type', 'category_wise_affiliate')->first()->status )
- {
- foreach ( $order->orderDetails as $key => $orderDetail )
- {
- $amount = 0;
- if ( $orderDetail->product_referral_code != NULL )
- {
- $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
- if ( $referred_by_user != NULL )
- {
- if ( AffiliateOption::where('type', 'category_wise_affiliate')->first()->details != NULL )
- {
- foreach ( json_decode(AffiliateOption::where('type', 'category_wise_affiliate')->first()->details) as $key => $value )
- {
- if ( $value->category_id == $orderDetail->product->category->id )
- {
- if ( $value->commission_type == 'amount' )
- {
- $amount = $value->commission;
- }
- else
- {
- $amount = ( $value->commission * $orderDetail->price ) / 100;
- }
- }
- }
- }
- $affiliate_user = $referred_by_user->affiliate_user;
- if ( $affiliate_user != NULL )
- {
- $affiliate_user->balance += $amount;
- $affiliate_user->save();
- // Affiliate log
- $affiliate_log = new AffiliateLog;
- if ( $order->user_id != NULL )
- {
- $affiliate_log->user_id = $order->user_id;
- }
- else
- {
- $affiliate_log->guest_id = $order->guest_id;
- }
- $affiliate_log->referred_by_user = $referred_by_user->id;
- $affiliate_log->amount = $amount;
- $affiliate_log->order_id = $order->id;
- $affiliate_log->order_detail_id = $orderDetail->id;
- $affiliate_log->affiliate_type = 'category_wise_affiliate';
- $affiliate_log->save();
- }
- }
- }
- }
- }
- }
- }
- public function processAffiliateStats( $affiliate_user_id, $no_click = 0, $no_item = 0, $no_delivered = 0, $no_cancel = 0 ) {
- $affiliate_stats = AffiliateStats::whereDate('created_at', Carbon::today())
- ->where("affiliate_user_id", $affiliate_user_id)
- ->first();
- if ( !$affiliate_stats )
- {
- $affiliate_stats = new AffiliateStats;
- $affiliate_stats->no_of_order_item = 0;
- $affiliate_stats->no_of_delivered = 0;
- $affiliate_stats->no_of_cancel = 0;
- $affiliate_stats->no_of_click = 0;
- }
- $affiliate_stats->no_of_order_item += $no_item;
- $affiliate_stats->no_of_delivered += $no_delivered;
- $affiliate_stats->no_of_cancel += $no_cancel;
- $affiliate_stats->no_of_click += $no_click;
- $affiliate_stats->affiliate_user_id = $affiliate_user_id;
- // dd($affiliate_stats);
- $affiliate_stats->save();
- // foreach($order->orderDetails as $key => $orderDetail) {
- // $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
- //
- // if($referred_by_user != null) {
- // if($orderDetail->delivery_status == 'delivered') {
- // $affiliate_stats->no_of_delivered++;
- // } if($orderDetail->delivery_status == 'cancelled') {
- // $affiliate_stats->no_of_cancel++;
- // }
- //
- // $affiliate_stats->affiliate_user_id = $referred_by_user->id;
- // dd($affiliate_stats);
- // $affiliate_stats->save();
- // }
- // }
- }
- public function refferal_users() {
- // $refferal_users = User::where('referred_by', '!=', NULL)->paginate(10);
- $refferal_users = User::where('leader_id', '!=', 0)->with([
- 'referrer' => function ( $one )
- {
- $one->with([
- 'referrer' => function ( $two )
- {
- $two->with([
- 'referrer' => function ( $two )
- {
- $two->with([ 'referrer' ]);
- },
- ]);
- },
- ]);
- },
- ])->orderBy('id', 'desc')->paginate(10);
- return view('affiliate.refferal_users', compact('refferal_users'));
- }
- // Affiliate Withdraw Request
- public function withdraw_request_store( Request $request ) {
- $withdraw_request = new AffiliateWithdrawRequest;
- $withdraw_request->user_id = Auth::user()->id;
- $withdraw_request->amount = $request->amount;
- $withdraw_request->status = 0;
- if ( $withdraw_request->save() )
- {
- $affiliate_user = AffiliateUser::where('user_id', Auth::user()->id)->first();
- $affiliate_user->balance = $affiliate_user->balance - $request->amount;
- $affiliate_user->save();
- flash(translate('New withdraw request created successfully'))->success();
- return redirect()->route('affiliate.user.withdraw_request_history');
- }
- else
- {
- flash(translate('Something went wrong'))->error();
- return back();
- }
- }
- public function affiliate_withdraw_requests() {
- $affiliate_withdraw_requests = AffiliateWithdrawRequest::orderBy('id', 'desc')->paginate(10);
- return view('affiliate.affiliate_withdraw_requests', compact('affiliate_withdraw_requests'));
- }
- public function affiliate_withdraw_modal( Request $request ) {
- $affiliate_withdraw_request = AffiliateWithdrawRequest::findOrFail($request->id);
- $affiliate_user = AffiliateUser::where('user_id', $affiliate_withdraw_request->user_id)->first();
- return view('affiliate.affiliate_withdraw_modal', compact('affiliate_withdraw_request', 'affiliate_user'));
- }
- public function withdraw_request_payment_store( Request $request ) {
- $affiliate_payment = new AffiliatePayment;
- $affiliate_payment->affiliate_user_id = $request->affiliate_user_id;
- $affiliate_payment->amount = $request->amount;
- $affiliate_payment->payment_method = $request->payment_method;
- $affiliate_payment->save();
- if ( $request->has('affiliate_withdraw_request_id') )
- {
- $affiliate_withdraw_request = AffiliateWithdrawRequest::findOrFail($request->affiliate_withdraw_request_id);
- $affiliate_withdraw_request->status = 1;
- $affiliate_withdraw_request->save();
- }
- flash(translate('Payment completed'))->success();
- return back();
- }
- public function reject_withdraw_request( $id ) {
- $affiliate_withdraw_request = AffiliateWithdrawRequest::findOrFail($id);
- $affiliate_withdraw_request->status = 2;
- if ( $affiliate_withdraw_request->save() )
- {
- $affiliate_user = AffiliateUser::where('user_id', $affiliate_withdraw_request->user_id)->first();
- $affiliate_user->balance = $affiliate_user->balance + $affiliate_withdraw_request->amount;
- $affiliate_user->save();
- flash(translate('Affiliate withdraw request has been rejected successfully'))->success();
- return redirect()->route('affiliate.withdraw_requests');
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- public function commissiontrain( Request $request ) {
- foreach ( $request->types as $key => $type )
- {
- $lang = NULL;
- if ( gettype($type) == 'array' )
- {
- $lang = array_key_first($type);
- $type = $type[$lang];
- $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
- }
- else
- {
- $business_settings = BusinessSetting::where('type', $type)->first();
- }
- if ( $business_settings != NULL )
- {
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- else
- {
- $business_settings = new BusinessSetting;
- $business_settings->type = $type;
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- }
- Artisan::call('cache:clear');
- flash(translate("Settings updated successfully"))->success();
- return back();
- }
- public function commissionstore( Request $request ) {
- foreach ( $request->types as $key => $type )
- {
- $lang = NULL;
- if ( gettype($type) == 'array' )
- {
- $lang = array_key_first($type);
- $type = $type[$lang];
- $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
- }
- else
- {
- $business_settings = BusinessSetting::where('type', $type)->first();
- }
- if ( $business_settings != NULL )
- {
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- else
- {
- $business_settings = new BusinessSetting;
- $business_settings->type = $type;
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- }
- Artisan::call('cache:clear');
- flash(translate("Settings updated successfully"))->success();
- return back();
- }
- public function commissionrecharge( Request $request ) {
- foreach ( $request->types as $key => $type )
- {
- $lang = NULL;
- if ( gettype($type) == 'array' )
- {
- $lang = array_key_first($type);
- $type = $type[$lang];
- $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
- }
- else
- {
- $business_settings = BusinessSetting::where('type', $type)->first();
- }
- if ( $business_settings != NULL )
- {
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- else
- {
- $business_settings = new BusinessSetting;
- $business_settings->type = $type;
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- }
- Artisan::call('cache:clear');
- flash(translate("Settings updated successfully"))->success();
- return back();
- }
- public function commissionpeople( Request $request ) {
- foreach ( $request->types as $key => $type )
- {
- $lang = NULL;
- if ( gettype($type) == 'array' )
- {
- $lang = array_key_first($type);
- $type = $type[$lang];
- $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
- }
- else
- {
- $business_settings = BusinessSetting::where('type', $type)->first();
- }
- if ( $business_settings != NULL )
- {
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- else
- {
- $business_settings = new BusinessSetting;
- $business_settings->type = $type;
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- }
- Artisan::call('cache:clear');
- flash(translate("Settings updated successfully"))->success();
- return back();
- }
- public function commission( Request $request ) {
- foreach ( $request->types as $key => $type )
- {
- $lang = NULL;
- if ( gettype($type) == 'array' )
- {
- $lang = array_key_first($type);
- $type = $type[$lang];
- $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
- }
- else
- {
- $business_settings = BusinessSetting::where('type', $type)->first();
- }
- if ( $business_settings != NULL )
- {
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- else
- {
- $business_settings = new BusinessSetting;
- $business_settings->type = $type;
- if ( gettype($request[$type]) == 'array' )
- {
- $business_settings->value = json_encode($request[$type]);
- }
- else
- {
- $business_settings->value = $request[$type];
- }
- $business_settings->lang = $lang;
- $business_settings->save();
- }
- }
- Artisan::call('cache:clear');
- flash(translate("Settings updated successfully"))->success();
- return back();
- }
- public function seller_index( Request $request ) {
- //统计
- $statistics = [
- 'total_seller' => 0,
- 'total_orders' => 0,
- 'total_amount' => 0,
- 'total_brokerage' => 0,
- ];
- $shops = [];
- //总商家数
- $users_1 = User::where('pid', '=', Auth::user()->id)->where('user_type', '=', 'seller')->get();//下一级商家
- $statistics['total_seller'] += count($users_1);
- foreach ( $users_1 as $user_1 )
- {
- $users_2 = User::where('pid', '=', $user_1->id)->where('user_type', '=', 'seller')->get();//下二级商家
- $statistics['total_seller'] += count($users_2);
- //查询所有订单
- $orders_1 = $orders = Order::where('seller_id', $user_1->id)->get();
- $statistics['total_orders'] += count($orders_1);
- $one = [
- 'shop_name' => $user_1->shop->name,
- 'order_number' => count($orders_1),
- 'brokerage' =>0,
- 'level' => '一级',
- ];
- foreach ( $orders_1 as $order_1 )
- {
- $statistics['total_amount'] += $order_1->grand_total;
- if (get_setting('commission_status')) $statistics['total_brokerage'] += $order_1->grand_total*get_setting('commission_ratio_level_1')/100;
- $one['brokerage'] += $order_1->grand_total*get_setting('commission_ratio_level_1')/100;
- }
- $shops[] = $one;
- foreach ( $users_2 as $user_2 )
- {
- $users_3 = User::where('pid', '=', $user_2->id)->where('user_type', '=', 'seller')->get();//下二级商家
- $statistics['total_seller'] += count($users_3);
- //查询所有订单
- $orders_2 = $orders = Order::where('seller_id', $user_2->id)->get();
- $statistics['total_orders'] += count($orders_2);
- $two = [
- 'shop_name' => $user_2->shop->name,
- 'order_number' => count($orders_2),
- 'brokerage' =>0,
- 'level' => '二级',
- ];
- foreach ( $orders_2 as $order_2 )
- {
- $statistics['total_amount'] += $order_2->grand_total;
- if (get_setting('commission_status')) $statistics['total_brokerage'] += $order_2->grand_total*get_setting('commission_ratio_level_2')/100;
- $two['brokerage'] += $order_2->grand_total*get_setting('commission_ratio_level_2')/100;
- }
- $shops[] = $two;
- foreach ( $users_3 as $user_3 )
- {
- //查询所有订单
- $orders_3 = $orders = Order::where('seller_id', $user_3->id)->get();
- $statistics['total_orders'] += count($orders_3);
- $three = [
- 'shop_name' => $user_3->shop->name,
- 'order_number' => count($orders_3),
- 'brokerage' =>0,
- 'level' => '三级',
- ];
- foreach ( $orders_3 as $order_3 )
- {
- $statistics['total_amount'] += $order_3->grand_total;
- if (get_setting('commission_status')) $statistics['total_brokerage'] += $order_3->grand_total*get_setting('commission_ratio_level_3')/100;
- $three['brokerage'] += $order_3->grand_total*get_setting('commission_ratio_level_3')/100;
- }
- $shops[] = $three;
- }
- }
- }
- $affiliate_logs = AffiliateLog::where('referred_by_user', Auth::user()->id)->with(['order'=>function($one){
- $one->with([
- 'details' => function ( $two )
- {
- $two->with([
- 'product',
- ]);
- },
- ]);
- }])->latest()->paginate(10);
- $query = AffiliateStats::query();
- $query = $query->select(
- DB::raw('SUM(no_of_click) AS count_click, SUM(no_of_order_item) AS count_item, SUM(no_of_delivered) AS count_delivered, SUM(no_of_cancel) AS count_cancel')
- );
- if ( $request->type == 'Today' )
- {
- $query->whereDate('created_at', Carbon::today());
- }
- else if ( $request->type == '7' || $request->type == '30' )
- {
- $query->whereRaw('created_at <= NOW() AND created_at >= DATE_SUB(created_at, INTERVAL ' . $request->type . ' DAY)');
- }
- $query->where('affiliate_user_id', Auth::user()->id);
- $affliate_stats = $query->first();
- $type = $request->type;
- $url = $request->root().'/shops/create?leader_id='.Auth::user()->id;
- return view('affiliate.seller.index', compact('affiliate_logs', 'affliate_stats', 'type', 'url', 'statistics', 'shops'));
- }
- }
|