164 lines
5.7 KiB
PHP
164 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Merchant\Controllers\Coupon;
|
|
|
|
use App\Merchant\Controllers\Controller;
|
|
use App\Merchant\Exporters\CouponExport;
|
|
use App\Models\Coupon;
|
|
use Auth;
|
|
use Illuminate\Http\Request;
|
|
|
|
class IndexController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$user = Auth::guard('merchant')->user();
|
|
|
|
$outlet = $request->outlet;
|
|
$status = $request->status;
|
|
$redemptionCode = $request->redemptionCode;
|
|
$start = $request->start;
|
|
$end = $request->end;
|
|
$thirdPartyGoodsId = $request->thirdPartyGoodsId;
|
|
$action = $request->action ?? 'search';
|
|
|
|
if ($start) {
|
|
$start = $start . ' 00:00:00';
|
|
}
|
|
|
|
if ($end) {
|
|
$end = $end . ' 23:59:59';
|
|
}
|
|
|
|
$coupons = Coupon::where('user_id', $user->id)
|
|
->when($outlet, function ($q) use ($outlet) {
|
|
$q->whereHas('outlet', function ($q) use ($outlet) {
|
|
$q->whereHas('info', function ($q) use ($outlet) {
|
|
$q->where('nickname', 'like', "%{$outlet}%");
|
|
});
|
|
});
|
|
})
|
|
->when($redemptionCode, function ($q) use ($redemptionCode) {
|
|
$q->where('redemptionCode', $redemptionCode);
|
|
})
|
|
->when($thirdPartyGoodsId, function ($q) use ($thirdPartyGoodsId) {
|
|
$q->where('thirdPartyGoodsId', $thirdPartyGoodsId);
|
|
})
|
|
->when(is_numeric($status), function ($query) use ($status) {
|
|
if ($status == 4) {
|
|
$query->where('is_profit', 1);
|
|
|
|
} else {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
})
|
|
->when($start && $end, function ($query) use ($start, $end) {
|
|
$query->whereBetween('created_at', [$start, $end]);
|
|
})
|
|
->when($start && !$end, function ($query) use ($start) {
|
|
$query->where('created_at', '>', $start);
|
|
})
|
|
->when(!$start && $end, function ($query) use ($end) {
|
|
$query->where('created_at', '<', $end);
|
|
})
|
|
->whereIn('status', [2, 3])
|
|
->orderBy('created_at', 'desc')->orderBy('id', 'desc')->paginate();
|
|
|
|
$all = Coupon::query()
|
|
->where('user_id', $user->id)
|
|
->when($outlet, function ($q) use ($outlet) {
|
|
$q->whereHas('outlet', function ($q) use ($outlet) {
|
|
$q->whereHas('info', function ($q) use ($outlet) {
|
|
$q->where('nickname', 'like', "%{$outlet}%");
|
|
});
|
|
});
|
|
})
|
|
->when($redemptionCode, function ($q) use ($redemptionCode) {
|
|
$q->where('redemptionCode', $redemptionCode);
|
|
})
|
|
->when(is_numeric($status), function ($query) use ($status) {
|
|
$query->where('status', $status);
|
|
}, function ($query) {
|
|
$query->whereIn('status', [2, 3]);
|
|
})
|
|
->when($start && $end, function ($query) use ($start, $end) {
|
|
$query->whereBetween('created_at', [$start, $end]);
|
|
})
|
|
->when($start, function ($query) use ($start) {
|
|
$query->where('created_at', '>', $start);
|
|
})
|
|
->when($end, function ($query) use ($end) {
|
|
$query->where('created_at', '<', $end);
|
|
})
|
|
->orderBy('created_at', 'desc')->orderBy('id', 'desc')->get();
|
|
|
|
$pass = $all->whereIn('status', [2, 4])->all();
|
|
$reject = $all->where('status', 3)->all();
|
|
$pass = collect($pass);
|
|
$reject = collect($reject);
|
|
|
|
$data = [
|
|
'all' => $all->count(),
|
|
'pass' => $pass->count(),
|
|
'reject' => $reject->count(),
|
|
'price' => $pass->sum('price'),
|
|
'profit' => $pass->sum('profit'),
|
|
'hasPrice' => $pass->where('is_profit', 1)->sum('profit'),
|
|
];
|
|
|
|
if ($action == 'search') {
|
|
return view('Merchant::coupon.index', compact('coupons', 'data'));
|
|
|
|
} else {
|
|
return (new CouponExport($request->all(), $user))->download();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 按照日期分润
|
|
* @param Request $request [description]
|
|
* @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
* @author 玄尘 2020-03-11
|
|
*/
|
|
public function profits(Request $request)
|
|
{
|
|
$user = Auth::guard('merchant')->user();
|
|
|
|
if ($request->isMethod('POST')) {
|
|
$date = $request->date;
|
|
|
|
$list = Coupon::where('user_id', $user->id)->whereDate('created_at', $date)->where('status', 2)->get();
|
|
if ($list->isEmpty()) {
|
|
return $this->error('分润失败!没有可处理的数据');
|
|
}
|
|
if (Coupon::where('user_id', $user->id)->whereDate('created_at', $date)->where('status', 2)->update(['status' => 4])) {
|
|
return $this->success('分润成功!');
|
|
} else {
|
|
return $this->error('分润失败!');
|
|
}
|
|
} else {
|
|
return view('Merchant::coupon.profits');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 分润
|
|
* @author 玄尘 2020-03-11
|
|
* @param Coupon $coupon [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function profit(Coupon $coupon)
|
|
{
|
|
if ($coupon->status == 2) {
|
|
$coupon->status = 4;
|
|
$coupon->save();
|
|
return $this->success('分润成功');
|
|
} else {
|
|
return $this->error('分润失败');
|
|
}
|
|
}
|
|
|
|
}
|