AffiliateController.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\BusinessSetting;
  4. use Artisan;
  5. use Illuminate\Http\Request;
  6. use App\Models\AffiliateOption;
  7. use App\Models\Order;
  8. use App\Models\AffiliateConfig;
  9. use App\Models\AffiliateUser;
  10. use App\Models\AffiliatePayment;
  11. use App\Models\AffiliateWithdrawRequest;
  12. use App\Models\AffiliateLog;
  13. use App\Models\AffiliateStats;
  14. use Carbon\Carbon;
  15. use App\Models\User;
  16. use App\Models\Customer;
  17. use App\Models\Category;
  18. use Auth;
  19. use DB;
  20. use Hash;
  21. use Illuminate\Auth\Events\Registered;
  22. use function collect;
  23. use function count;
  24. use function dd;
  25. use function get_setting;
  26. use function print_r;
  27. use function with;
  28. class AffiliateController extends Controller
  29. {
  30. //
  31. public function index() {
  32. return view('affiliate.index');
  33. }
  34. public function affiliate_option_store( Request $request ) {
  35. //dd($request->all());
  36. $affiliate_option = AffiliateOption::where('type', $request->type)->first();
  37. if ( $affiliate_option == NULL )
  38. {
  39. $affiliate_option = new AffiliateOption;
  40. }
  41. $affiliate_option->type = $request->type;
  42. $commision_details = [];
  43. if ( $request->type == 'user_registration_first_purchase' )
  44. {
  45. $affiliate_option->percentage = $request->percentage;
  46. }
  47. else if ( $request->type == 'product_sharing' )
  48. {
  49. $commision_details['commission'] = $request->amount;
  50. $commision_details['commission_type'] = $request->amount_type;
  51. }
  52. else if ( $request->type == 'category_wise_affiliate' )
  53. {
  54. foreach ( Category::all() as $category )
  55. {
  56. $data['category_id'] = $request['categories_id_' . $category->id];
  57. $data['commission'] = $request['commison_amounts_' . $category->id];
  58. $data['commission_type'] = $request['commison_types_' . $category->id];
  59. array_push($commision_details, $data);
  60. }
  61. }
  62. else if ( $request->type == 'max_affiliate_limit' )
  63. {
  64. $affiliate_option->percentage = $request->percentage;
  65. }
  66. $affiliate_option->details = json_encode($commision_details);
  67. if ( $request->has('status') )
  68. {
  69. $affiliate_option->status = 1;
  70. if ( $request->type == 'product_sharing' )
  71. {
  72. $affiliate_option_status_update = AffiliateOption::where('type', 'category_wise_affiliate')->first();
  73. $affiliate_option_status_update->status = 0;
  74. $affiliate_option_status_update->save();
  75. }
  76. if ( $request->type == 'category_wise_affiliate' )
  77. {
  78. $affiliate_option_status_update = AffiliateOption::where('type', 'product_sharing')->first();
  79. $affiliate_option_status_update->status = 0;
  80. $affiliate_option_status_update->save();
  81. }
  82. }
  83. else
  84. {
  85. $affiliate_option->status = 0;
  86. }
  87. $affiliate_option->save();
  88. flash("This has been updated successfully")->success();
  89. return back();
  90. }
  91. public function configs() {
  92. return view('affiliate.configs');
  93. }
  94. public function config_store( Request $request ) {
  95. if ( $request->type == 'validation_time' )
  96. {
  97. //affiliate validation time
  98. $affiliate_config = AffiliateConfig::where('type', $request->type)->first();
  99. if ( $affiliate_config == NULL )
  100. {
  101. $affiliate_config = new AffiliateConfig;
  102. }
  103. $affiliate_config->type = $request->type;
  104. $affiliate_config->value = $request[$request->type];
  105. $affiliate_config->save();
  106. flash("Validation time updated successfully")->success();
  107. }
  108. else
  109. {
  110. $form = [];
  111. $select_types = [ 'select', 'multi_select', 'radio' ];
  112. $j = 0;
  113. for ( $i = 0; $i < count($request->type); $i++ )
  114. {
  115. $item['type'] = $request->type[$i];
  116. $item['label'] = $request->label[$i];
  117. if ( in_array($request->type[$i], $select_types) )
  118. {
  119. $item['options'] = json_encode($request['options_' . $request->option[$j]]);
  120. $j++;
  121. }
  122. array_push($form, $item);
  123. }
  124. $affiliate_config = AffiliateConfig::where('type', 'verification_form')->first();
  125. $affiliate_config->value = json_encode($form);
  126. flash("Verification form updated successfully")->success();
  127. }
  128. if ( $affiliate_config->save() )
  129. {
  130. return back();
  131. }
  132. }
  133. public function apply_for_affiliate( Request $request ) {
  134. if ( Auth::check() && AffiliateUser::where('user_id', Auth::user()->id)->first() != NULL )
  135. {
  136. flash(translate("You are already an affiliate user!"))->warning();
  137. return back();
  138. }
  139. return view('affiliate.frontend.apply_for_affiliate');
  140. }
  141. public function affiliate_logs_admin() {
  142. $affiliate_logs = AffiliateLog::latest()->paginate(10);
  143. return view('affiliate.affiliate_logs', compact('affiliate_logs'));
  144. }
  145. public function store_affiliate_user( Request $request ) {
  146. if ( !Auth::check() )
  147. {
  148. if ( User::where('email', $request->email)->first() != NULL )
  149. {
  150. flash(translate('Email already exists!'))->error();
  151. return back();
  152. }
  153. if ( $request->password == $request->password_confirmation )
  154. {
  155. $user = new User;
  156. $user->name = $request->name;
  157. $user->email = $request->email;
  158. $user->user_type = "customer";
  159. $user->password = Hash::make($request->password);
  160. $user->save();
  161. $customer = new Customer;
  162. $customer->user_id = $user->id;
  163. $customer->save();
  164. auth()->login($user, false);
  165. if ( get_setting('email_verification') != 1 )
  166. {
  167. $user->email_verified_at = date('Y-m-d H:m:s');
  168. $user->save();
  169. }
  170. else
  171. {
  172. event(new Registered($user));
  173. }
  174. }
  175. else
  176. {
  177. flash(translate('Sorry! Password did not match.'))->error();
  178. return back();
  179. }
  180. }
  181. $affiliate_user = Auth::user()->affiliate_user;
  182. if ( $affiliate_user == NULL )
  183. {
  184. $affiliate_user = new AffiliateUser;
  185. $affiliate_user->user_id = Auth::user()->id;
  186. }
  187. $data = [];
  188. $i = 0;
  189. foreach ( json_decode(AffiliateConfig::where('type', 'verification_form')->first()->value) as $key => $element )
  190. {
  191. $item = [];
  192. if ( $element->type == 'text' )
  193. {
  194. $item['type'] = 'text';
  195. $item['label'] = $element->label;
  196. $item['value'] = $request['element_' . $i];
  197. }
  198. else if ( $element->type == 'select' || $element->type == 'radio' )
  199. {
  200. $item['type'] = 'select';
  201. $item['label'] = $element->label;
  202. $item['value'] = $request['element_' . $i];
  203. }
  204. else if ( $element->type == 'multi_select' )
  205. {
  206. $item['type'] = 'multi_select';
  207. $item['label'] = $element->label;
  208. $item['value'] = json_encode($request['element_' . $i]);
  209. }
  210. else if ( $element->type == 'file' )
  211. {
  212. $item['type'] = 'file';
  213. $item['label'] = $element->label;
  214. $item['value'] = $request['element_' . $i]->store('uploads/affiliate_verification_form');
  215. }
  216. array_push($data, $item);
  217. $i++;
  218. }
  219. $affiliate_user->informations = json_encode($data);
  220. if ( $affiliate_user->save() )
  221. {
  222. flash(translate('Your verification request has been submitted successfully!'))->success();
  223. return redirect()->route('home');
  224. }
  225. flash(translate('Sorry! Something went wrong.'))->error();
  226. return back();
  227. }
  228. public function users() {
  229. $affiliate_users = AffiliateUser::paginate(12);
  230. return view('affiliate.users', compact('affiliate_users'));
  231. }
  232. public function show_verification_request( $id ) {
  233. $affiliate_user = AffiliateUser::findOrFail($id);
  234. return view('affiliate.show_verification_request', compact('affiliate_user'));
  235. }
  236. public function approve_user( $id ) {
  237. $affiliate_user = AffiliateUser::findOrFail($id);
  238. $affiliate_user->status = 1;
  239. if ( $affiliate_user->save() )
  240. {
  241. flash(translate('Affiliate user has been approved successfully'))->success();
  242. return redirect()->route('affiliate.users');
  243. }
  244. flash(translate('Something went wrong'))->error();
  245. return back();
  246. }
  247. public function reject_user( $id ) {
  248. $affiliate_user = AffiliateUser::findOrFail($id);
  249. $affiliate_user->status = 0;
  250. $affiliate_user->informations = NULL;
  251. if ( $affiliate_user->save() )
  252. {
  253. flash(translate('Affiliate user request has been rejected successfully'))->success();
  254. return redirect()->route('affiliate.users');
  255. }
  256. flash(translate('Something went wrong'))->error();
  257. return back();
  258. }
  259. public function updateApproved( Request $request ) {
  260. $affiliate_user = AffiliateUser::findOrFail($request->id);
  261. $affiliate_user->status = $request->status;
  262. if ( $affiliate_user->save() )
  263. {
  264. return 1;
  265. }
  266. return 0;
  267. }
  268. public function payment_modal( Request $request ) {
  269. $affiliate_user = AffiliateUser::findOrFail($request->id);
  270. return view('affiliate.payment_modal', compact('affiliate_user'));
  271. }
  272. public function payment_store( Request $request ) {
  273. $affiliate_payment = new AffiliatePayment;
  274. $affiliate_payment->affiliate_user_id = $request->affiliate_user_id;
  275. $affiliate_payment->amount = $request->amount;
  276. $affiliate_payment->payment_method = $request->payment_method;
  277. $affiliate_payment->save();
  278. $affiliate_user = AffiliateUser::findOrFail($request->affiliate_user_id);
  279. $affiliate_user->balance -= $request->amount;
  280. $affiliate_user->save();
  281. flash(translate('Payment completed'))->success();
  282. return back();
  283. }
  284. public function payment_history( $id ) {
  285. $affiliate_user = AffiliateUser::findOrFail(decrypt($id));
  286. $affiliate_payments = $affiliate_user->affiliate_payments();
  287. return view('affiliate.payment_history', compact('affiliate_payments', 'affiliate_user'));
  288. }
  289. public function user_index( Request $request ) {
  290. $affiliate_logs = AffiliateLog::where('referred_by_user', Auth::user()->id)->latest()->paginate(10);
  291. $query = AffiliateStats::query();
  292. $query = $query->select(
  293. 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')
  294. );
  295. if ( $request->type == 'Today' )
  296. {
  297. $query->whereDate('created_at', Carbon::today());
  298. }
  299. else if ( $request->type == '7' || $request->type == '30' )
  300. {
  301. $query->whereRaw('created_at <= NOW() AND created_at >= DATE_SUB(created_at, INTERVAL ' . $request->type . ' DAY)');
  302. }
  303. $query->where('affiliate_user_id', Auth::user()->id);
  304. $affliate_stats = $query->first();
  305. $type = $request->type;
  306. return view('affiliate.frontend.index', compact('affiliate_logs', 'affliate_stats', 'type'));
  307. }
  308. // payment history for user
  309. public function user_payment_history() {
  310. $affiliate_user = Auth::user()->affiliate_user;
  311. $affiliate_payments = $affiliate_user->affiliate_payments();
  312. return view('affiliate.frontend.payment_history', compact('affiliate_payments'));
  313. }
  314. // withdraw request history for user
  315. public function user_withdraw_request_history() {
  316. $affiliate_user = Auth::user()->affiliate_user;
  317. $affiliate_withdraw_requests = AffiliateWithdrawRequest::where('user_id', Auth::user()->id)->orderBy('id', 'desc')->paginate(10);
  318. return view('affiliate.frontend.withdraw_request_history', compact('affiliate_withdraw_requests'));
  319. }
  320. public function payment_settings() {
  321. $affiliate_user = Auth::user()->affiliate_user;
  322. return view('affiliate.frontend.payment_settings', compact('affiliate_user'));
  323. }
  324. public function payment_settings_store( Request $request ) {
  325. $affiliate_user = Auth::user()->affiliate_user;
  326. $affiliate_user->paypal_email = $request->paypal_email;
  327. $affiliate_user->bank_information = $request->bank_information;
  328. $affiliate_user->save();
  329. flash(translate('Affiliate payment settings has been updated successfully'))->success();
  330. return redirect()->route('affiliate.user.index');
  331. }
  332. public function processAffiliatePoints( Order $order ) {
  333. if ( addon_is_activated('affiliate_system') )
  334. {
  335. if ( AffiliateOption::where('type', 'user_registration_first_purchase')->first()->status )
  336. {
  337. if ( $order->user != NULL && $order->user->orders->count() == 1 )
  338. {
  339. if ( $order->user->referred_by != NULL )
  340. {
  341. $user = User::find($order->user->referred_by);
  342. if ( $user != NULL )
  343. {
  344. $amount = ( AffiliateOption::where('type', 'user_registration_first_purchase')->first()->percentage * $order->grand_total ) / 100;
  345. $affiliate_user = $user->affiliate_user;
  346. if ( $affiliate_user != NULL )
  347. {
  348. $affiliate_user->balance += $amount;
  349. $affiliate_user->save();
  350. // Affiliate log
  351. $affiliate_log = new AffiliateLog;
  352. $affiliate_log->user_id = $order->user_id;
  353. $affiliate_log->referred_by_user = $order->user->referred_by;
  354. $affiliate_log->amount = $amount;
  355. $affiliate_log->order_id = $order->id;
  356. $affiliate_log->affiliate_type = 'user_registration_first_purchase';
  357. $affiliate_log->save();
  358. }
  359. }
  360. }
  361. }
  362. }
  363. if ( AffiliateOption::where('type', 'product_sharing')->first()->status )
  364. {
  365. foreach ( $order->orderDetails as $key => $orderDetail )
  366. {
  367. $amount = 0;
  368. if ( $orderDetail->product_referral_code != NULL )
  369. {
  370. $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
  371. if ( $referred_by_user != NULL )
  372. {
  373. if ( AffiliateOption::where('type', 'product_sharing')->first()->details != NULL && json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission_type == 'amount' )
  374. {
  375. $amount = json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission;
  376. }
  377. else if ( AffiliateOption::where('type', 'product_sharing')->first()->details != NULL && json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission_type == 'percent' )
  378. {
  379. $amount = ( json_decode(AffiliateOption::where('type', 'product_sharing')->first()->details)->commission * $orderDetail->price ) / 100;
  380. }
  381. $affiliate_user = $referred_by_user->affiliate_user;
  382. if ( $affiliate_user != NULL )
  383. {
  384. $affiliate_user->balance += $amount;
  385. $affiliate_user->save();
  386. // Affiliate log
  387. $affiliate_log = new AffiliateLog;
  388. if ( $order->user_id != NULL )
  389. {
  390. $affiliate_log->user_id = $order->user_id;
  391. }
  392. else
  393. {
  394. $affiliate_log->guest_id = $order->guest_id;
  395. }
  396. $affiliate_log->referred_by_user = $referred_by_user->id;
  397. $affiliate_log->amount = $amount;
  398. $affiliate_log->order_id = $order->id;
  399. $affiliate_log->order_detail_id = $orderDetail->id;
  400. $affiliate_log->affiliate_type = 'product_sharing';
  401. $affiliate_log->save();
  402. }
  403. }
  404. }
  405. }
  406. }
  407. else if ( AffiliateOption::where('type', 'category_wise_affiliate')->first()->status )
  408. {
  409. foreach ( $order->orderDetails as $key => $orderDetail )
  410. {
  411. $amount = 0;
  412. if ( $orderDetail->product_referral_code != NULL )
  413. {
  414. $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
  415. if ( $referred_by_user != NULL )
  416. {
  417. if ( AffiliateOption::where('type', 'category_wise_affiliate')->first()->details != NULL )
  418. {
  419. foreach ( json_decode(AffiliateOption::where('type', 'category_wise_affiliate')->first()->details) as $key => $value )
  420. {
  421. if ( $value->category_id == $orderDetail->product->category->id )
  422. {
  423. if ( $value->commission_type == 'amount' )
  424. {
  425. $amount = $value->commission;
  426. }
  427. else
  428. {
  429. $amount = ( $value->commission * $orderDetail->price ) / 100;
  430. }
  431. }
  432. }
  433. }
  434. $affiliate_user = $referred_by_user->affiliate_user;
  435. if ( $affiliate_user != NULL )
  436. {
  437. $affiliate_user->balance += $amount;
  438. $affiliate_user->save();
  439. // Affiliate log
  440. $affiliate_log = new AffiliateLog;
  441. if ( $order->user_id != NULL )
  442. {
  443. $affiliate_log->user_id = $order->user_id;
  444. }
  445. else
  446. {
  447. $affiliate_log->guest_id = $order->guest_id;
  448. }
  449. $affiliate_log->referred_by_user = $referred_by_user->id;
  450. $affiliate_log->amount = $amount;
  451. $affiliate_log->order_id = $order->id;
  452. $affiliate_log->order_detail_id = $orderDetail->id;
  453. $affiliate_log->affiliate_type = 'category_wise_affiliate';
  454. $affiliate_log->save();
  455. }
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. public function processAffiliateStats( $affiliate_user_id, $no_click = 0, $no_item = 0, $no_delivered = 0, $no_cancel = 0 ) {
  463. $affiliate_stats = AffiliateStats::whereDate('created_at', Carbon::today())
  464. ->where("affiliate_user_id", $affiliate_user_id)
  465. ->first();
  466. if ( !$affiliate_stats )
  467. {
  468. $affiliate_stats = new AffiliateStats;
  469. $affiliate_stats->no_of_order_item = 0;
  470. $affiliate_stats->no_of_delivered = 0;
  471. $affiliate_stats->no_of_cancel = 0;
  472. $affiliate_stats->no_of_click = 0;
  473. }
  474. $affiliate_stats->no_of_order_item += $no_item;
  475. $affiliate_stats->no_of_delivered += $no_delivered;
  476. $affiliate_stats->no_of_cancel += $no_cancel;
  477. $affiliate_stats->no_of_click += $no_click;
  478. $affiliate_stats->affiliate_user_id = $affiliate_user_id;
  479. // dd($affiliate_stats);
  480. $affiliate_stats->save();
  481. // foreach($order->orderDetails as $key => $orderDetail) {
  482. // $referred_by_user = User::where('referral_code', $orderDetail->product_referral_code)->first();
  483. //
  484. // if($referred_by_user != null) {
  485. // if($orderDetail->delivery_status == 'delivered') {
  486. // $affiliate_stats->no_of_delivered++;
  487. // } if($orderDetail->delivery_status == 'cancelled') {
  488. // $affiliate_stats->no_of_cancel++;
  489. // }
  490. //
  491. // $affiliate_stats->affiliate_user_id = $referred_by_user->id;
  492. // dd($affiliate_stats);
  493. // $affiliate_stats->save();
  494. // }
  495. // }
  496. }
  497. public function refferal_users() {
  498. // $refferal_users = User::where('referred_by', '!=', NULL)->paginate(10);
  499. $refferal_users = User::where('leader_id', '!=', 0)->with([
  500. 'referrer' => function ( $one )
  501. {
  502. $one->with([
  503. 'referrer' => function ( $two )
  504. {
  505. $two->with([
  506. 'referrer' => function ( $two )
  507. {
  508. $two->with([ 'referrer' ]);
  509. },
  510. ]);
  511. },
  512. ]);
  513. },
  514. ])->orderBy('id', 'desc')->paginate(10);
  515. return view('affiliate.refferal_users', compact('refferal_users'));
  516. }
  517. // Affiliate Withdraw Request
  518. public function withdraw_request_store( Request $request ) {
  519. $withdraw_request = new AffiliateWithdrawRequest;
  520. $withdraw_request->user_id = Auth::user()->id;
  521. $withdraw_request->amount = $request->amount;
  522. $withdraw_request->status = 0;
  523. if ( $withdraw_request->save() )
  524. {
  525. $affiliate_user = AffiliateUser::where('user_id', Auth::user()->id)->first();
  526. $affiliate_user->balance = $affiliate_user->balance - $request->amount;
  527. $affiliate_user->save();
  528. flash(translate('New withdraw request created successfully'))->success();
  529. return redirect()->route('affiliate.user.withdraw_request_history');
  530. }
  531. else
  532. {
  533. flash(translate('Something went wrong'))->error();
  534. return back();
  535. }
  536. }
  537. public function affiliate_withdraw_requests() {
  538. $affiliate_withdraw_requests = AffiliateWithdrawRequest::orderBy('id', 'desc')->paginate(10);
  539. return view('affiliate.affiliate_withdraw_requests', compact('affiliate_withdraw_requests'));
  540. }
  541. public function affiliate_withdraw_modal( Request $request ) {
  542. $affiliate_withdraw_request = AffiliateWithdrawRequest::findOrFail($request->id);
  543. $affiliate_user = AffiliateUser::where('user_id', $affiliate_withdraw_request->user_id)->first();
  544. return view('affiliate.affiliate_withdraw_modal', compact('affiliate_withdraw_request', 'affiliate_user'));
  545. }
  546. public function withdraw_request_payment_store( Request $request ) {
  547. $affiliate_payment = new AffiliatePayment;
  548. $affiliate_payment->affiliate_user_id = $request->affiliate_user_id;
  549. $affiliate_payment->amount = $request->amount;
  550. $affiliate_payment->payment_method = $request->payment_method;
  551. $affiliate_payment->save();
  552. if ( $request->has('affiliate_withdraw_request_id') )
  553. {
  554. $affiliate_withdraw_request = AffiliateWithdrawRequest::findOrFail($request->affiliate_withdraw_request_id);
  555. $affiliate_withdraw_request->status = 1;
  556. $affiliate_withdraw_request->save();
  557. }
  558. flash(translate('Payment completed'))->success();
  559. return back();
  560. }
  561. public function reject_withdraw_request( $id ) {
  562. $affiliate_withdraw_request = AffiliateWithdrawRequest::findOrFail($id);
  563. $affiliate_withdraw_request->status = 2;
  564. if ( $affiliate_withdraw_request->save() )
  565. {
  566. $affiliate_user = AffiliateUser::where('user_id', $affiliate_withdraw_request->user_id)->first();
  567. $affiliate_user->balance = $affiliate_user->balance + $affiliate_withdraw_request->amount;
  568. $affiliate_user->save();
  569. flash(translate('Affiliate withdraw request has been rejected successfully'))->success();
  570. return redirect()->route('affiliate.withdraw_requests');
  571. }
  572. flash(translate('Something went wrong'))->error();
  573. return back();
  574. }
  575. public function commissiontrain( Request $request ) {
  576. foreach ( $request->types as $key => $type )
  577. {
  578. $lang = NULL;
  579. if ( gettype($type) == 'array' )
  580. {
  581. $lang = array_key_first($type);
  582. $type = $type[$lang];
  583. $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
  584. }
  585. else
  586. {
  587. $business_settings = BusinessSetting::where('type', $type)->first();
  588. }
  589. if ( $business_settings != NULL )
  590. {
  591. if ( gettype($request[$type]) == 'array' )
  592. {
  593. $business_settings->value = json_encode($request[$type]);
  594. }
  595. else
  596. {
  597. $business_settings->value = $request[$type];
  598. }
  599. $business_settings->lang = $lang;
  600. $business_settings->save();
  601. }
  602. else
  603. {
  604. $business_settings = new BusinessSetting;
  605. $business_settings->type = $type;
  606. if ( gettype($request[$type]) == 'array' )
  607. {
  608. $business_settings->value = json_encode($request[$type]);
  609. }
  610. else
  611. {
  612. $business_settings->value = $request[$type];
  613. }
  614. $business_settings->lang = $lang;
  615. $business_settings->save();
  616. }
  617. }
  618. Artisan::call('cache:clear');
  619. flash(translate("Settings updated successfully"))->success();
  620. return back();
  621. }
  622. public function commissionstore( Request $request ) {
  623. foreach ( $request->types as $key => $type )
  624. {
  625. $lang = NULL;
  626. if ( gettype($type) == 'array' )
  627. {
  628. $lang = array_key_first($type);
  629. $type = $type[$lang];
  630. $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
  631. }
  632. else
  633. {
  634. $business_settings = BusinessSetting::where('type', $type)->first();
  635. }
  636. if ( $business_settings != NULL )
  637. {
  638. if ( gettype($request[$type]) == 'array' )
  639. {
  640. $business_settings->value = json_encode($request[$type]);
  641. }
  642. else
  643. {
  644. $business_settings->value = $request[$type];
  645. }
  646. $business_settings->lang = $lang;
  647. $business_settings->save();
  648. }
  649. else
  650. {
  651. $business_settings = new BusinessSetting;
  652. $business_settings->type = $type;
  653. if ( gettype($request[$type]) == 'array' )
  654. {
  655. $business_settings->value = json_encode($request[$type]);
  656. }
  657. else
  658. {
  659. $business_settings->value = $request[$type];
  660. }
  661. $business_settings->lang = $lang;
  662. $business_settings->save();
  663. }
  664. }
  665. Artisan::call('cache:clear');
  666. flash(translate("Settings updated successfully"))->success();
  667. return back();
  668. }
  669. public function commissionrecharge( Request $request ) {
  670. foreach ( $request->types as $key => $type )
  671. {
  672. $lang = NULL;
  673. if ( gettype($type) == 'array' )
  674. {
  675. $lang = array_key_first($type);
  676. $type = $type[$lang];
  677. $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
  678. }
  679. else
  680. {
  681. $business_settings = BusinessSetting::where('type', $type)->first();
  682. }
  683. if ( $business_settings != NULL )
  684. {
  685. if ( gettype($request[$type]) == 'array' )
  686. {
  687. $business_settings->value = json_encode($request[$type]);
  688. }
  689. else
  690. {
  691. $business_settings->value = $request[$type];
  692. }
  693. $business_settings->lang = $lang;
  694. $business_settings->save();
  695. }
  696. else
  697. {
  698. $business_settings = new BusinessSetting;
  699. $business_settings->type = $type;
  700. if ( gettype($request[$type]) == 'array' )
  701. {
  702. $business_settings->value = json_encode($request[$type]);
  703. }
  704. else
  705. {
  706. $business_settings->value = $request[$type];
  707. }
  708. $business_settings->lang = $lang;
  709. $business_settings->save();
  710. }
  711. }
  712. Artisan::call('cache:clear');
  713. flash(translate("Settings updated successfully"))->success();
  714. return back();
  715. }
  716. public function commissionpeople( Request $request ) {
  717. foreach ( $request->types as $key => $type )
  718. {
  719. $lang = NULL;
  720. if ( gettype($type) == 'array' )
  721. {
  722. $lang = array_key_first($type);
  723. $type = $type[$lang];
  724. $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
  725. }
  726. else
  727. {
  728. $business_settings = BusinessSetting::where('type', $type)->first();
  729. }
  730. if ( $business_settings != NULL )
  731. {
  732. if ( gettype($request[$type]) == 'array' )
  733. {
  734. $business_settings->value = json_encode($request[$type]);
  735. }
  736. else
  737. {
  738. $business_settings->value = $request[$type];
  739. }
  740. $business_settings->lang = $lang;
  741. $business_settings->save();
  742. }
  743. else
  744. {
  745. $business_settings = new BusinessSetting;
  746. $business_settings->type = $type;
  747. if ( gettype($request[$type]) == 'array' )
  748. {
  749. $business_settings->value = json_encode($request[$type]);
  750. }
  751. else
  752. {
  753. $business_settings->value = $request[$type];
  754. }
  755. $business_settings->lang = $lang;
  756. $business_settings->save();
  757. }
  758. }
  759. Artisan::call('cache:clear');
  760. flash(translate("Settings updated successfully"))->success();
  761. return back();
  762. }
  763. public function commission( Request $request ) {
  764. foreach ( $request->types as $key => $type )
  765. {
  766. $lang = NULL;
  767. if ( gettype($type) == 'array' )
  768. {
  769. $lang = array_key_first($type);
  770. $type = $type[$lang];
  771. $business_settings = BusinessSetting::where('type', $type)->where('lang', $lang)->first();
  772. }
  773. else
  774. {
  775. $business_settings = BusinessSetting::where('type', $type)->first();
  776. }
  777. if ( $business_settings != NULL )
  778. {
  779. if ( gettype($request[$type]) == 'array' )
  780. {
  781. $business_settings->value = json_encode($request[$type]);
  782. }
  783. else
  784. {
  785. $business_settings->value = $request[$type];
  786. }
  787. $business_settings->lang = $lang;
  788. $business_settings->save();
  789. }
  790. else
  791. {
  792. $business_settings = new BusinessSetting;
  793. $business_settings->type = $type;
  794. if ( gettype($request[$type]) == 'array' )
  795. {
  796. $business_settings->value = json_encode($request[$type]);
  797. }
  798. else
  799. {
  800. $business_settings->value = $request[$type];
  801. }
  802. $business_settings->lang = $lang;
  803. $business_settings->save();
  804. }
  805. }
  806. Artisan::call('cache:clear');
  807. flash(translate("Settings updated successfully"))->success();
  808. return back();
  809. }
  810. public function seller_index( Request $request ) {
  811. //统计
  812. $statistics = [
  813. 'total_seller' => 0,
  814. 'total_orders' => 0,
  815. 'total_amount' => 0,
  816. 'total_brokerage' => 0,
  817. ];
  818. $shops = [];
  819. //总商家数
  820. $users_1 = User::where('pid', '=', Auth::user()->id)->where('user_type', '=', 'seller')->get();//下一级商家
  821. $statistics['total_seller'] += count($users_1);
  822. foreach ( $users_1 as $user_1 )
  823. {
  824. $users_2 = User::where('pid', '=', $user_1->id)->where('user_type', '=', 'seller')->get();//下二级商家
  825. $statistics['total_seller'] += count($users_2);
  826. //查询所有订单
  827. $orders_1 = $orders = Order::where('seller_id', $user_1->id)->get();
  828. $statistics['total_orders'] += count($orders_1);
  829. $one = [
  830. 'shop_name' => $user_1->shop->name,
  831. 'order_number' => count($orders_1),
  832. 'brokerage' =>0,
  833. 'level' => '一级',
  834. ];
  835. foreach ( $orders_1 as $order_1 )
  836. {
  837. $statistics['total_amount'] += $order_1->grand_total;
  838. if (get_setting('commission_status')) $statistics['total_brokerage'] += $order_1->grand_total*get_setting('commission_ratio_level_1')/100;
  839. $one['brokerage'] += $order_1->grand_total*get_setting('commission_ratio_level_1')/100;
  840. }
  841. $shops[] = $one;
  842. foreach ( $users_2 as $user_2 )
  843. {
  844. $users_3 = User::where('pid', '=', $user_2->id)->where('user_type', '=', 'seller')->get();//下二级商家
  845. $statistics['total_seller'] += count($users_3);
  846. //查询所有订单
  847. $orders_2 = $orders = Order::where('seller_id', $user_2->id)->get();
  848. $statistics['total_orders'] += count($orders_2);
  849. $two = [
  850. 'shop_name' => $user_2->shop->name,
  851. 'order_number' => count($orders_2),
  852. 'brokerage' =>0,
  853. 'level' => '二级',
  854. ];
  855. foreach ( $orders_2 as $order_2 )
  856. {
  857. $statistics['total_amount'] += $order_2->grand_total;
  858. if (get_setting('commission_status')) $statistics['total_brokerage'] += $order_2->grand_total*get_setting('commission_ratio_level_2')/100;
  859. $two['brokerage'] += $order_2->grand_total*get_setting('commission_ratio_level_2')/100;
  860. }
  861. $shops[] = $two;
  862. foreach ( $users_3 as $user_3 )
  863. {
  864. //查询所有订单
  865. $orders_3 = $orders = Order::where('seller_id', $user_3->id)->get();
  866. $statistics['total_orders'] += count($orders_3);
  867. $three = [
  868. 'shop_name' => $user_3->shop->name,
  869. 'order_number' => count($orders_3),
  870. 'brokerage' =>0,
  871. 'level' => '三级',
  872. ];
  873. foreach ( $orders_3 as $order_3 )
  874. {
  875. $statistics['total_amount'] += $order_3->grand_total;
  876. if (get_setting('commission_status')) $statistics['total_brokerage'] += $order_3->grand_total*get_setting('commission_ratio_level_3')/100;
  877. $three['brokerage'] += $order_3->grand_total*get_setting('commission_ratio_level_3')/100;
  878. }
  879. $shops[] = $three;
  880. }
  881. }
  882. }
  883. $affiliate_logs = AffiliateLog::where('referred_by_user', Auth::user()->id)->with(['order'=>function($one){
  884. $one->with([
  885. 'details' => function ( $two )
  886. {
  887. $two->with([
  888. 'product',
  889. ]);
  890. },
  891. ]);
  892. }])->latest()->paginate(10);
  893. $query = AffiliateStats::query();
  894. $query = $query->select(
  895. 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')
  896. );
  897. if ( $request->type == 'Today' )
  898. {
  899. $query->whereDate('created_at', Carbon::today());
  900. }
  901. else if ( $request->type == '7' || $request->type == '30' )
  902. {
  903. $query->whereRaw('created_at <= NOW() AND created_at >= DATE_SUB(created_at, INTERVAL ' . $request->type . ' DAY)');
  904. }
  905. $query->where('affiliate_user_id', Auth::user()->id);
  906. $affliate_stats = $query->first();
  907. $type = $request->type;
  908. $url = $request->root().'/shops/create?leader_id='.Auth::user()->id;
  909. return view('affiliate.seller.index', compact('affiliate_logs', 'affliate_stats', 'type', 'url', 'statistics', 'shops'));
  910. }
  911. }