CommissionController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\CommissionHistory;
  4. use App\Models\User;
  5. use Illuminate\Http\Request;
  6. use App\Models\SellerWithdrawRequest;
  7. use App\Models\Seller;
  8. use App\Models\Payment;
  9. use App\Models\Shop;
  10. use Illuminate\Support\Carbon;
  11. use Session;
  12. class CommissionController extends Controller
  13. {
  14. public function refuse(Request $request){
  15. $withdrawRequest = SellerWithdrawRequest::find($request->withdraw_request_id);
  16. if (!$withdrawRequest) {
  17. flash(translate('Something went wrong'))->error();
  18. return back();
  19. }
  20. $user = User::find($withdrawRequest->user_id);
  21. if (!$user) {
  22. flash(translate('Something went wrong'))->error();
  23. return back();
  24. }
  25. if( $withdrawRequest->type ==1 )
  26. {
  27. //更新为拒绝
  28. $withdrawRequest->status = '2';
  29. $withdrawRequest->viewed = '1';
  30. $withdrawRequest->remarks = $request->remarks;
  31. $withdrawRequest->save();
  32. //退款回支付账户
  33. $user->balance = $user->balance+$withdrawRequest->amount;
  34. $user->save();
  35. return redirect()->route('sellers.index');
  36. }
  37. else
  38. {
  39. $withdrawRequest->status = '2';
  40. $withdrawRequest->viewed = '1';
  41. $withdrawRequest->remarks = $request->remarks;
  42. $withdrawRequest->save();
  43. $shop = Shop::findOrFail( $user->shop->id );
  44. $shop->bzj_money = $shop->bzj_money +$withdrawRequest->amount;
  45. $shop->save();
  46. return redirect()->route('sellers.index');
  47. }
  48. }
  49. public function refuse234(Request $request){
  50. $withdrawRequest = SellerWithdrawRequest::find($request->withdraw_request_id);
  51. if (!$withdrawRequest) {
  52. flash(translate('Something went wrong'))->error();
  53. return back();
  54. }
  55. $user = User::find($withdrawRequest->user_id);
  56. if (!$user) {
  57. flash(translate('Something went wrong'))->error();
  58. return back();
  59. }
  60. //更新为拒绝
  61. $withdrawRequest->status = '2';
  62. $withdrawRequest->viewed = '1';
  63. $withdrawRequest->save();
  64. //退款回支付账户
  65. $user->balance = $user->balance+$withdrawRequest->amount;
  66. $user->save();
  67. return redirect()->route('sellers.index');
  68. }
  69. //redirect to payment controllers according to selected payment gateway for seller payment
  70. public function pay_to_seller(Request $request)
  71. {
  72. /*shop_id: 1
  73. amount: 10
  74. txn_code:
  75. id: 1
  76. */
  77. $shopid = $request->id;
  78. $shop = Shop::findOrFail( $shopid);
  79. $amount = floatval($request->amount);
  80. if( $amount == 0 )
  81. {
  82. flash(translate('Something went wrong'))->error();
  83. return back();
  84. }
  85. $user = User::find($shop->user_id);
  86. if (!$user) {
  87. flash(translate('Something went wrong'))->error();
  88. return back();
  89. }
  90. $user->balance = $user->balance + $amount;
  91. $user->save();
  92. $payment = new Payment;
  93. $payment->seller_id = $user->id;
  94. $payment->amount = $amount;
  95. $payment->payment_method = 'Pay From admin';
  96. $payment->txn_code = date("YmdHis");
  97. $payment->payment_details = null;
  98. $payment->save();
  99. flash(translate('Payment completed'))->success();
  100. return redirect()->route('sellers.index');
  101. echo "<PRE>";
  102. #print_r( $request->);
  103. exit;
  104. $withdrawRequest = SellerWithdrawRequest::find($request->withdraw_request_id);
  105. if (!$withdrawRequest) {
  106. flash(translate('Something went wrong'))->error();
  107. return back();
  108. }
  109. $user = User::find($withdrawRequest->user_id);
  110. if (!$user) {
  111. flash(translate('Something went wrong'))->error();
  112. return back();
  113. }
  114. $data['shop_id'] = $request->shop_id;
  115. $data['amount'] = $request->amount;
  116. $data['payment_method'] = $request->payment_option;
  117. $data['payment_withdraw'] = $request->payment_withdraw;
  118. $data['withdraw_request_id'] = $request->withdraw_request_id;
  119. if ($request->txn_code != null) {
  120. $data['txn_code'] = $request->txn_code;
  121. } else {
  122. $data['txn_code'] = null;
  123. }
  124. $request->session()->put('payment_type', 'seller_payment');
  125. $request->session()->put('payment_data', $data);
  126. if ($request->payment_option == 'cash') {
  127. return $this->seller_payment_done($request->session()->get('payment_data'), null, $withdrawRequest, $user);
  128. } elseif ($request->payment_option == 'bank_payment') {
  129. return $this->seller_payment_done($request->session()->get('payment_data'), null, $withdrawRequest, $user);
  130. } else {
  131. $payment_data = $request->session()->get('payment_data');
  132. // $shop = Shop::findOrFail($payment_data['shop_id']);
  133. // $shop->admin_to_pay = $shop->admin_to_pay - $payment_data['amount'];
  134. // $shop->save();
  135. $user->balance = $user->balance + $payment_data['amount'];
  136. $user->save();
  137. $payment = new Payment;
  138. $payment->seller_id = $user->id;
  139. $payment->amount = $payment_data['amount'];
  140. $payment->payment_method = 'Seller paid to admin';
  141. $payment->txn_code = $payment_data['txn_code'];
  142. $payment->payment_details = null;
  143. $payment->save();
  144. flash(translate('Payment completed'))->success();
  145. return redirect()->route('sellers.index');
  146. }
  147. }
  148. public function pay_to_seller2(Request $request)
  149. {
  150. /*shop_id: 1
  151. amount: 10
  152. txn_code:
  153. id: 1
  154. */
  155. $withdrawRequest = SellerWithdrawRequest::find($request->seller_withdraw_request_id);
  156. $withdrawRequest->remarks = $request->remarks;
  157. if (!$withdrawRequest) {
  158. flash(translate('Something went wrong'))->error();
  159. return back();
  160. }
  161. $user = User::find($withdrawRequest->user_id);
  162. if (!$user) {
  163. flash(translate('Something went wrong'))->error();
  164. return back();
  165. }
  166. $data['shop_id'] = $request->shop_id;
  167. $data['amount'] = $request->amount;
  168. $data['payment_method'] = $request->payment_option;
  169. $data['payment_withdraw'] = $request->payment_withdraw;
  170. $data['withdraw_request_id'] = $request->withdraw_request_id;
  171. if ($request->txn_code != null) {
  172. $data['txn_code'] = $request->txn_code;
  173. } else {
  174. $data['txn_code'] = null;
  175. }
  176. $request->session()->put('payment_type', 'seller_payment');
  177. $request->session()->put('payment_data', $data);
  178. if ($request->payment_option == 'cash') {
  179. return $this->seller_payment_done($request->session()->get('payment_data'), null, $withdrawRequest, $user);
  180. } else if ($request->payment_option == 'bank_payment') {
  181. return $this->seller_payment_done($request->session()->get('payment_data'), null, $withdrawRequest, $user);
  182. } else if($request->payment_option == 'usdt_payment') {
  183. return $this->seller_payment_done($request->session()->get('payment_data'), null, $withdrawRequest, $user);
  184. } else {
  185. $payment_data = $request->session()->get('payment_data');
  186. // $shop = Shop::findOrFail($payment_data['shop_id']);
  187. // $shop->admin_to_pay = $shop->admin_to_pay - $payment_data['amount'];
  188. // $shop->save();
  189. $user->balance = $user->balance + $payment_data['amount'];
  190. $user->save();
  191. $payment = new Payment;
  192. $payment->seller_id = $user->id;
  193. $payment->amount = $payment_data['amount'];
  194. $payment->payment_method = 'Seller paid to admin';
  195. $payment->txn_code = $payment_data['txn_code'];
  196. $payment->payment_details = null;
  197. $payment->t_type = $withdrawRequest->t_type;
  198. $payment->save();
  199. flash(translate('Payment completed'))->success();
  200. return redirect()->route('sellers.withdraw_requests_all.index');
  201. }
  202. }
  203. //redirects to this method after successfull seller payment
  204. public function seller_payment_done($payment_data, $payment_details, $withdrawRequest, $user)
  205. {
  206. $withdrawRequest = $withdrawRequest ?: SellerWithdrawRequest::find($payment_data['withdraw_request_id']);
  207. $user = $user ?: User::find($withdrawRequest->user_id);
  208. if (!$withdrawRequest || !$user) {
  209. flash(translate('Something went wrong'))->error();
  210. return back();
  211. }
  212. // $shop = Shop::findOrFail($payment_data['shop_id']);
  213. // $shop->admin_to_pay = $shop->admin_to_pay - $payment_data['amount'];
  214. // $shop->save();
  215. // $user->balance = $user->balance + $payment_data['amount'];
  216. // $user->save();
  217. $payment = new Payment;
  218. $payment->seller_id = $user->id;
  219. $payment->amount = $payment_data['amount'];
  220. $payment->payment_method = $payment_data['payment_method'];
  221. $payment->txn_code = $payment_data['txn_code'];
  222. $payment->payment_details = $payment_details;
  223. $payment->t_type = $withdrawRequest->t_type;
  224. $payment->save();
  225. if ($payment_data['payment_withdraw'] == 'withdraw_request') {
  226. $seller_withdraw_request = SellerWithdrawRequest::findOrFail($payment_data['withdraw_request_id']);
  227. $seller_withdraw_request->status = '1';
  228. $seller_withdraw_request->viewed = '1';
  229. $seller_withdraw_request->remarks = $withdrawRequest->remarks;
  230. $seller_withdraw_request->save();
  231. }
  232. Session::forget('payment_data');
  233. Session::forget('payment_type');
  234. if ($payment_data['payment_withdraw'] == 'withdraw_request') {
  235. flash(translate('Payment completed'))->success();
  236. return redirect()->route('withdraw_requests_all');
  237. } else {
  238. flash(translate('Payment completed'))->success();
  239. return redirect()->route('sellers.index');
  240. }
  241. }
  242. //calculate seller commission after payment
  243. public function calculateCommission($order)
  244. {
  245. if ($order->payment_type == 'cash_on_delivery') {
  246. foreach ($order->orderDetails as $orderDetail) {
  247. $orderDetail->payment_status = 'paid';
  248. $orderDetail->save();
  249. $commission_percentage = 0;
  250. if (get_setting('vendor_commission_activation')) {
  251. if (get_setting('category_wise_commission')) {
  252. $commission_percentage = $orderDetail->product->category->commision_rate;
  253. } else if ($orderDetail->product->user->user_type == 'seller') {
  254. $commission_percentage = get_setting('vendor_commission');
  255. }
  256. }
  257. if ($orderDetail->product->user->user_type == 'seller') {
  258. $shop = $orderDetail->product->user->shop;
  259. $admin_commission = ($orderDetail->price * $commission_percentage) / 100;
  260. if (get_setting('product_manage_by_admin') == 1) {
  261. $shop_earning = ($orderDetail->tax + $orderDetail->price) - $admin_commission;
  262. $shop->admin_to_pay += $shop_earning;
  263. } else {
  264. $shop_earning = ($orderDetail->tax + $orderDetail->shipping_cost + $orderDetail->price) - $admin_commission;
  265. $shop->admin_to_pay -= $admin_commission;
  266. }
  267. $shop->save();
  268. $commission_history = new CommissionHistory;
  269. $commission_history->order_id = $order->id;
  270. $commission_history->order_detail_id = $orderDetail->id;
  271. $commission_history->seller_id = $orderDetail->seller_id;
  272. $commission_history->admin_commission = $admin_commission;
  273. $commission_history->seller_earning = $shop_earning;
  274. $commission_history->save();
  275. }
  276. }
  277. } else {
  278. foreach ($order->orderDetails as $orderDetail) {
  279. $orderDetail->payment_status = 'paid';
  280. $orderDetail->save();
  281. $commission_percentage = 0;
  282. if (get_setting('vendor_commission_activation')) {
  283. if (get_setting('category_wise_commission')) {
  284. $commission_percentage = $orderDetail->product->category->commision_rate;
  285. } else if ($orderDetail->product->user->user_type == 'seller') {
  286. $commission_percentage = get_setting('vendor_commission');
  287. }
  288. }
  289. if ($orderDetail->product->user->user_type == 'seller') {
  290. $shop = $orderDetail->product->user->shop;
  291. $admin_commission = ($orderDetail->price * $commission_percentage) / 100;
  292. $shop_earning_copy = 0;
  293. if (!$order->product_storehouse_total) {
  294. if (get_setting('product_manage_by_admin') == 1) {
  295. $shop_earning = ($orderDetail->tax + $orderDetail->price) - $admin_commission;
  296. $shop->admin_to_pay += $shop_earning;
  297. } else {
  298. $shop_earning = ($orderDetail->tax + $orderDetail->shipping_cost + $orderDetail->price) - $admin_commission;
  299. $shop->admin_to_pay += $shop_earning;
  300. }
  301. $shop->save();
  302. $shop_earning_copy = $shop_earning;
  303. }
  304. $commission_history = new CommissionHistory;
  305. $commission_history->order_id = $order->id;
  306. $commission_history->order_detail_id = $orderDetail->id;
  307. $commission_history->seller_id = $orderDetail->seller_id;
  308. $commission_history->admin_commission = $admin_commission;
  309. $commission_history->seller_earning = $shop_earning_copy;
  310. $commission_history->save();
  311. }
  312. }
  313. // // 保存订单冻结资金过期时间
  314. // if ($order->product_storehouse_total > 0) {
  315. // $freezeDays = get_setting('frozen_funds_unfrozen_days', 15);
  316. // $order->freeze_expired_at = Carbon::now()->addDays($freezeDays)->timestamp;
  317. // $order->save();
  318. // }
  319. // $shop = $order->shop;
  320. // $shop->freeze_funds += $order->product_storehouse_total; // 冻结资金
  321. // $shop->save();
  322. if ($order->shop != null) {
  323. $order->shop->admin_to_pay -= $order->coupon_discount;
  324. $order->shop->save();
  325. }
  326. }
  327. }
  328. }