CouponController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\Coupon;
  4. use Illuminate\Http\Request;
  5. use Maatwebsite\Excel\Facades\Excel;
  6. use Response;
  7. use Redirect;
  8. use Session;
  9. use DB;
  10. use Log;
  11. /**
  12. * 优惠券控制器
  13. * Class LoginController
  14. *
  15. * @package App\Http\Controllers
  16. */
  17. class CouponController extends Controller
  18. {
  19. // 优惠券列表
  20. public function couponList(Request $request)
  21. {
  22. $view['couponList'] = Coupon::query()->where('is_del', 0)->orderBy('status', 'asc')->orderBy('id', 'desc')->paginate(10);
  23. return Response::view('coupon/couponList', $view);
  24. }
  25. // 添加商品
  26. public function addCoupon(Request $request)
  27. {
  28. if ($request->method() == 'POST') {
  29. $name = $request->get('name');
  30. $type = $request->get('type', 1);
  31. $usage = $request->get('usage', 1);
  32. $num = $request->get('num', 1);
  33. $amount = $request->get('amount');
  34. $discount = $request->get('discount');
  35. $available_start = $request->get('available_start');
  36. $available_end = $request->get('available_end');
  37. if (empty($num) || (empty($amount) && empty($discount)) || empty($available_start) || empty($available_end)) {
  38. Session::flash('errorMsg', '请填写完整');
  39. return Redirect::back()->withInput();
  40. }
  41. if (strtotime($available_start) >= strtotime($available_end)) {
  42. Session::flash('errorMsg', '有效期范围错误');
  43. return Redirect::back()->withInput();
  44. }
  45. // 商品LOGO
  46. $logo = '';
  47. if ($request->hasFile('logo')) {
  48. $file = $request->file('logo');
  49. $fileType = $file->getClientOriginalExtension();
  50. $logoName = date('YmdHis') . mt_rand(1000, 2000) . '.' . $fileType;
  51. $move = $file->move(base_path() . '/public/upload/image/coupon/', $logoName);
  52. $logo = $move ? '/upload/image/coupon/' . $logoName : '';
  53. }
  54. DB::beginTransaction();
  55. try {
  56. for ($i = 0; $i < $num; $i++) {
  57. $obj = new Coupon();
  58. $obj->name = $name;
  59. $obj->sn = strtoupper(makeRandStr(7));
  60. $obj->logo = $logo;
  61. $obj->type = $type;
  62. $obj->usage = $usage;
  63. $obj->amount = empty($amount) ? 0 : $amount;
  64. $obj->discount = empty($discount) ? 0 : $discount;
  65. $obj->available_start = strtotime(date('Y-m-d 0:0:0', strtotime($available_start)));
  66. $obj->available_end = strtotime(date('Y-m-d 23:59:59', strtotime($available_end)));
  67. $obj->status = 0;
  68. $obj->save();
  69. }
  70. DB::commit();
  71. Session::flash('successMsg', '生成成功');
  72. } catch (\Exception $e) {
  73. DB::rollBack();
  74. Log::error('生成优惠券失败:' . $e->getMessage());
  75. Session::flash('errorMsg', '生成失败:' . $e->getMessage());
  76. }
  77. return Redirect::to('coupon/addCoupon');
  78. } else {
  79. return Response::view('coupon/addCoupon');
  80. }
  81. }
  82. // 删除优惠券
  83. public function delCoupon(Request $request)
  84. {
  85. $id = $request->get('id');
  86. Coupon::query()->where('id', $id)->update(['is_del' => 1]);
  87. return Response::json(['status' => 'success', 'data' => '', 'message' => '删除成功']);
  88. }
  89. // 导出优惠券
  90. public function exportCoupon(Request $request)
  91. {
  92. $cashCouponList = Coupon::query()->where('is_del', 0)->where('status', 0)->where('type', 1)->get();
  93. $discountCouponList = Coupon::query()->where('is_del', 0)->where('status', 0)->where('type', 2)->get();
  94. $chargeCouponList = Coupon::query()->where('is_del', 0)->where('status', 0)->where('type', 3)->get();
  95. $filename = '卡券' . date('Ymd');
  96. Excel::create($filename, function ($excel) use ($cashCouponList, $discountCouponList, $chargeCouponList) {
  97. $excel->sheet('抵用券', function ($sheet) use ($cashCouponList) {
  98. $sheet->row(1, [
  99. '名称', '类型', '有效期', '券码', '面额'
  100. ]);
  101. if (!$cashCouponList->isEmpty()) {
  102. foreach ($cashCouponList as $k => $vo) {
  103. $sheet->row($k + 2, [
  104. $vo->name, $vo->type == 1 ? '一次性' : '可重复', date('Y-m-d', $vo->available_start) . ' ~ ' . date('Y-m-d', $vo->available_end), $vo->sn, $vo->amount
  105. ]);
  106. }
  107. }
  108. });
  109. $excel->sheet('折扣券', function ($sheet) use ($discountCouponList) {
  110. $sheet->row(1, [
  111. '名称', '类型', '有效期', '券码', '折扣'
  112. ]);
  113. if (!$discountCouponList->isEmpty()) {
  114. foreach ($discountCouponList as $k => $vo) {
  115. $sheet->row($k + 2, [
  116. $vo->name, $vo->type == 1 ? '一次性' : '可重复', date('Y-m-d', $vo->available_start) . ' ~ ' . date('Y-m-d', $vo->available_end), $vo->sn, $vo->discount
  117. ]);
  118. }
  119. }
  120. });
  121. $excel->sheet('充值券', function ($sheet) use ($chargeCouponList) {
  122. $sheet->row(1, [
  123. '名称', '类型', '有效期', '券码', '面额'
  124. ]);
  125. if (!$chargeCouponList->isEmpty()) {
  126. foreach ($chargeCouponList as $k => $vo) {
  127. $sheet->row($k + 2, [
  128. $vo->name, '一次性', date('Y-m-d', $vo->available_start) . ' ~ ' . date('Y-m-d', $vo->available_end), $vo->sn, $vo->amount
  129. ]);
  130. }
  131. }
  132. });
  133. })->export('xls');
  134. }
  135. }