244 lines
7.8 KiB
PHP
244 lines
7.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Activity;
|
|
use App\Models\Cart;
|
|
use App\Models\GoodsParams;
|
|
use App\Models\UserBaby;
|
|
use Auth;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\View;
|
|
use RuLong\Coupon\Models\CouponUserLog;
|
|
use RuLong\Order\Models\Order;
|
|
use RuLong\Order\Models\OrderDetail;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
public function __construct(Request $request)
|
|
{
|
|
parent::__construct($request);
|
|
$this->middleware('auth');
|
|
View::share('nav', 2);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$orders = Order::where('user_id', Auth::id())
|
|
->CouponAndActivity()
|
|
->where('state', '<>', Order::ORDER_CLOSED)
|
|
->orderBy('id', 'desc')
|
|
->get();
|
|
|
|
return view('orders.index', compact('orders'));
|
|
}
|
|
|
|
//活动和优惠券 未支付
|
|
public function unpay()
|
|
{
|
|
$orders = Order::Unpaid()
|
|
->CouponAndActivity()
|
|
->where('user_id', Auth::id())
|
|
->orderBy('id', 'desc')
|
|
->get();
|
|
return view('orders.index', compact('orders'));
|
|
}
|
|
|
|
//已支付报名课程和活动
|
|
public function paid()
|
|
{
|
|
$orders = Order::UnUse()
|
|
->CouponAndActivity()
|
|
->where('user_id', Auth::id())
|
|
->orderBy('id', 'desc')
|
|
->get();
|
|
|
|
return view('orders.index', compact('orders'));
|
|
}
|
|
|
|
//已使用的报名课程和活动
|
|
public function used()
|
|
{
|
|
$orders = Order::StateUsed()
|
|
->CouponAndActivity()
|
|
->where('user_id', Auth::id())
|
|
->orderBy('id', 'desc')
|
|
->get();
|
|
return view('orders.index', compact('orders'));
|
|
}
|
|
|
|
public function delivered()
|
|
{
|
|
$orders = Order::Delivered()
|
|
->where('user_id', Auth::id())
|
|
->orderBy('id', 'desc')
|
|
->get();
|
|
return view('orders.index', compact('orders'));
|
|
}
|
|
|
|
public function signed()
|
|
{
|
|
$orders = Order::Signed()
|
|
->where('user_id', Auth::id())
|
|
->orderBy('id', 'desc')
|
|
->get();
|
|
return view('orders.index', compact('orders'));
|
|
}
|
|
|
|
public function show($orderid)
|
|
{
|
|
$order = Order::where('orderid', $orderid)->firstOrFail();
|
|
return view('orders.show', compact('order'));
|
|
}
|
|
|
|
public function pay($orderid)
|
|
{
|
|
$order = Order::where('orderid', $orderid)->firstOrFail();
|
|
return view('orders.pay', compact('order'));
|
|
}
|
|
|
|
public function sign($orderid)
|
|
{
|
|
$order = Order::where('orderid', $orderid)->firstOrFail();
|
|
try {
|
|
$order->signin();
|
|
return $this->success('签收成功', route('orders.signed'));
|
|
} catch (\Exception $e) {
|
|
return $this->error('签收失败' . $e->getmessage());
|
|
}
|
|
}
|
|
|
|
public function delete($orderid)
|
|
{
|
|
$order = Order::where('orderid', $orderid)->firstOrFail();
|
|
try {
|
|
$order->close();
|
|
return $this->success('取消订单成功');
|
|
} catch (\Exception $e) {
|
|
return $this->error('取消订单失败' . $e->getmessage());
|
|
}
|
|
}
|
|
|
|
//创建课程订单
|
|
public function LessonStore(Request $request)
|
|
{
|
|
$request->validate([
|
|
'cart_ids.*' => 'required',
|
|
], [
|
|
'cart_ids.*.required' => '没有课程',
|
|
]);
|
|
|
|
if (!isset($request->cart_ids)) {
|
|
return $this->error('请选择课程后进行支付');
|
|
}
|
|
|
|
if (!$request->has('baby_id')) {
|
|
return $this->error('没有检测到宝宝信息,请先去录入宝宝信息。', route('baby', ['callback' => route('cart.index')]));
|
|
}
|
|
|
|
// $lesson_num = \Params::get('lesson_num');
|
|
// if ($lesson_num > count($request->cart_ids)) {
|
|
// return $this->error('请选够' . $lesson_num . '门课程后进行支付');
|
|
// }
|
|
|
|
$gift_nums = GoodsParams::where('stock', '>', 0)
|
|
->whereHas('goods', function ($q) {
|
|
$q->where('status', 1)->where('is_seller_gift', 1);
|
|
})
|
|
->count();
|
|
|
|
if ($gift_nums > 0 && !isset($request->gifts_id)) {
|
|
return $this->error('请选择赠品');
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($request) {
|
|
$baby = UserBaby::find($request->baby_id);
|
|
|
|
$item = [
|
|
'id' => $request->gifts_id ?? 0,
|
|
'item_type' => 'LESSON',
|
|
'name' => $baby->name,
|
|
'age' => $baby->age,
|
|
'mobile' => $baby->mobile,
|
|
];
|
|
|
|
$amount = \Params::get('apply_lesson_price');
|
|
|
|
$score_order = 0;
|
|
$score = 0;
|
|
$heavy = 0;
|
|
$windup_freight = 0;
|
|
$express_type = isset($request['express_type']) ? $request['express_type'] : 0;
|
|
$address = null;
|
|
$windup_freight = 0;
|
|
$items = [];
|
|
|
|
if ($request->coupon_id) {
|
|
$coupon = CouponUserLog::find($request->coupon_id);
|
|
if ($coupon) {
|
|
$score_order = $coupon->info->bouns;
|
|
}
|
|
} elseif (Auth::user()->identity->identity_id > 0) {
|
|
$amount -= 100;
|
|
}
|
|
|
|
foreach ($request->cart_ids as $key => $cart_id) {
|
|
$cart = Cart::find($cart_id);
|
|
if ($cart) {
|
|
array_push($items, new OrderDetail(['goods' => $cart->lesson, 'number' => 1]));
|
|
}
|
|
}
|
|
|
|
\Orders::create(Auth::id(), 1, $item, $items, $address, $request->remark, $amount, $score_order, 0, $express_type, $windup_freight, $coupon ?? []);
|
|
|
|
});
|
|
|
|
$order = Order::where('user_id', Auth::id())->where('state', 'UNPAY')->orderBy('created_at', 'desc')->first();
|
|
Auth::user()->info->update([
|
|
'baby_age' => $request->age,
|
|
'baby_name' => $request->name,
|
|
'baby_mobile' => $request->mobile,
|
|
]);
|
|
return $this->success('订单创建成功', route('pay.lesson', $order));
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->error('订单创建失败' . $e->getmessage());
|
|
}
|
|
}
|
|
|
|
public function activityStore(Request $request)
|
|
{
|
|
try {
|
|
DB::transaction(function () use ($request) {
|
|
$item = [
|
|
'id' => 0,
|
|
'item_type' => 'ACTIVITY',
|
|
];
|
|
|
|
$score = 0;
|
|
$score_order = 0;
|
|
$heavy = 0;
|
|
$windup_freight = 0;
|
|
$express_type = isset($request['express_type']) ? $request['express_type'] : 0;
|
|
$address = null;
|
|
$windup_freight = 0;
|
|
$items = [];
|
|
$activity = Activity::find($request->activity_id);
|
|
array_push($items, new OrderDetail(['goods' => $activity, 'number' => $request->number]));
|
|
$amount = $activity->price * $request->number;
|
|
\Orders::create(Auth::id(), 1, $item, $items, $address, $request->remark, $amount, $score_order, 0, $express_type, $windup_freight);
|
|
|
|
});
|
|
|
|
$order = Order::where('user_id', Auth::id())->where('state', 'UNPAY')->orderBy('created_at', 'desc')->first();
|
|
return $this->success('订单创建成功', route('pay.order', $order));
|
|
|
|
} catch (\Exception $e) {
|
|
return $this->error('订单创建失败' . $e->getmessage());
|
|
}
|
|
}
|
|
|
|
}
|