|
|
@@ -15,6 +15,7 @@ use App\Models\GoodsCategoryModel;
|
|
|
use App\Models\GoodsCollectModel;
|
|
|
use App\Models\GoodsModel;
|
|
|
use App\Models\GoodsSkuModel;
|
|
|
+use App\Models\MemberCouponModel;
|
|
|
use App\Models\MemberModel;
|
|
|
use App\Services\BaseService;
|
|
|
use App\Services\ConfigService;
|
|
|
@@ -232,20 +233,22 @@ class GoodsService extends BaseService
|
|
|
* @param $orderNo 订单号
|
|
|
* @return array|false
|
|
|
*/
|
|
|
- public function getOrderGoods($ids, $goods, $userId, $orderNo='', $discountPoint=0)
|
|
|
+ public function getOrderGoods($ids, $goods, $userId, $orderNo='', $discountPoint=0, $couponId=0)
|
|
|
{
|
|
|
if(empty($ids) || empty($goods)){
|
|
|
$this->error = '请选择商品';
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
// 用户信息
|
|
|
if(empty($orderNo)){
|
|
|
- $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
|
|
|
- ->select(['id','openid', 'status'])
|
|
|
+ $userInfo = MemberModel::with(['levelData'])->where(['id' => $userId, 'mark' => 1])
|
|
|
+ ->select(['id','openid','vip_expired','member_level', 'status'])
|
|
|
->first();
|
|
|
$status = isset($userInfo['status']) ? $userInfo['status'] : 0;
|
|
|
$openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
|
|
|
- $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
|
|
|
+ $vipExpired = isset($userInfo['vip_expired']) ? $userInfo['vip_expired'] : 0;
|
|
|
+ $levelData = isset($userInfo['level_data']) ? $userInfo['level_data'] : [];
|
|
|
if (empty($userInfo) || $status != 1) {
|
|
|
$this->error = 1045;
|
|
|
return false;
|
|
|
@@ -255,8 +258,24 @@ class GoodsService extends BaseService
|
|
|
$this->error = 1042;
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ // 有效会员
|
|
|
+ if($vipExpired != 0){
|
|
|
+ $discountPoint = isset($levelData['discount']) ? $levelData['discount'] : 0; // 会员折扣
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ $couponGoodsIds = [];
|
|
|
+ $couponInfo = [];
|
|
|
+ if($couponId){
|
|
|
+ $couponInfo = MemberCouponModel::where(['coupon_id'=>$couponId,'user_id'=>$userId,'mark'=>1])->first();
|
|
|
+ $couponType = isset($couponInfo['coupon_type'])?$couponInfo['coupon_type'] : 0;
|
|
|
+ $couponGoodsIds = isset($couponInfo['goods_ids'])&& $couponInfo['goods_ids']?explode(',', $couponInfo['goods_ids']) : [];
|
|
|
+ if(empty($couponInfo) || $couponType<=0){
|
|
|
+ $this->error = '优惠券无效';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
$list = $this->model->whereIn('id', $ids)
|
|
|
->where(['status'=>1,'mark'=>1])
|
|
|
@@ -265,9 +284,10 @@ class GoodsService extends BaseService
|
|
|
->keyBy('goods_id');
|
|
|
$list = $list? $list->toArray() : [];
|
|
|
if($list){
|
|
|
+ $isGoodsCoupon = false;
|
|
|
$skus = GoodsSkuModel::whereIn('goods_id', $ids)->select(['id','sku_name','price','stock'])->get()->keyBy('id');
|
|
|
$skus = $skus?$skus->toArray() :[];
|
|
|
- $result = ['discount_point'=>$discountPoint,'store_id'=>0,'discount_total'=>0.00,'delivery_fee'=>0.00,'goods_total'=>0,'order_total'=>0,'count'=>0,'goods'=>[]];
|
|
|
+ $result = ['discount_point'=>$discountPoint,'store_id'=>0,'coupon_id'=>$couponId,'coupon_total'=>0,'discount_total'=>0.00,'delivery_fee'=>0.00,'goods_total'=>0,'order_total'=>0,'count'=>0,'goods'=>[]];
|
|
|
foreach ($goods as $params){
|
|
|
$goodsId = isset($params['id'])?$params['id']:0;
|
|
|
$skuId = isset($params['sku_id'])?$params['sku_id']:0;
|
|
|
@@ -317,6 +337,25 @@ class GoodsService extends BaseService
|
|
|
$item['total'] = $price;
|
|
|
$item['num'] = $num;
|
|
|
$total = round($price * $num,2);
|
|
|
+
|
|
|
+ // 计算优惠
|
|
|
+ $couponData = $this->countCouponTotal($goodsId, $total, $couponInfo);
|
|
|
+ if(!$couponData){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否商品使用的优惠券
|
|
|
+ $couponTotal = isset($couponData['coupon_total'])?$couponData['coupon_total'] : 0;
|
|
|
+ $payTotal = isset($couponData['total'])?$couponData['total'] : 0;
|
|
|
+ if($payTotal){
|
|
|
+ $total = $payTotal;
|
|
|
+ $item['coupon_id'] = $couponId;
|
|
|
+ $item['coupon_total'] = $couponTotal;
|
|
|
+ $result['coupon_id'] = $couponId;
|
|
|
+ $result['coupon_total'] = $couponTotal;
|
|
|
+ $isGoodsCoupon = true;
|
|
|
+ }
|
|
|
+
|
|
|
$item['total'] = $total;
|
|
|
$item['thumb'] = $orderNo?get_image_path($item['thumb']):$item['thumb'];
|
|
|
$result['goods'][] = $item;
|
|
|
@@ -333,6 +372,18 @@ class GoodsService extends BaseService
|
|
|
$result['discount_total'] = moneyFormat($orderTotal - $result['order_total'],2);
|
|
|
}
|
|
|
|
|
|
+ // 非商品优惠券,整个订单的优惠券
|
|
|
+ if($couponId && !$isGoodsCoupon){
|
|
|
+ $couponData = $this->countCouponTotal(0, $orderTotal, $couponInfo);
|
|
|
+ if(!$couponData){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $couponTotal = isset($couponData['coupon_total'])?$couponData['coupon_total'] : 0;
|
|
|
+ $result['order_total'] = isset($couponData['total'])?$couponData['total'] : 0;
|
|
|
+ $result['coupon_total'] = $couponTotal;
|
|
|
+ }
|
|
|
+
|
|
|
$result['pay_total'] = moneyFormat($result['order_total'] + $result['delivery_fee'],2);
|
|
|
return $result;
|
|
|
}
|
|
|
@@ -341,6 +392,82 @@ class GoodsService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 优惠券计算
|
|
|
+ * @param $goodsId
|
|
|
+ * @param $total
|
|
|
+ * @param $couponInfo
|
|
|
+ * @return array|bool|int
|
|
|
+ */
|
|
|
+ public function countCouponTotal($goodsId, $total, $couponInfo)
|
|
|
+ {
|
|
|
+ $couponStatus = isset($couponInfo['status'])?$couponInfo['status'] : 0;
|
|
|
+ $couponType = isset($couponInfo['coupon_type'])?$couponInfo['coupon_type'] : 0;
|
|
|
+ $couponGoodsIds = isset($couponInfo['goods_ids'])&& $couponInfo['goods_ids']?explode(',', $couponInfo['goods_ids']) : [];
|
|
|
+ // 按商品或全平台(非购买券)
|
|
|
+ if($goodsId && $couponType != 20 && ($couponGoodsIds && !in_array($goodsId, $couponGoodsIds))){
|
|
|
+ $this->error = "优惠券非该商品使用";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($total <= 0 ){
|
|
|
+ $this->error = "消费金额错误";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($couponStatus != 1){
|
|
|
+ $this->error = "该优惠券已被使用";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $endTime = isset($couponInfo['end_time'])?$couponInfo['end_time']:0;
|
|
|
+ $startTime = isset($couponInfo['start_time'])?$couponInfo['start_time']:0;
|
|
|
+ if($startTime && time() < $startTime){
|
|
|
+ $this->error = '优惠券使用时间未到';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($endTime>0 && time() > $endTime){
|
|
|
+ $this->error = '优惠券已过期';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 满减券
|
|
|
+ if($couponType == 10){
|
|
|
+ $minPrice = isset($couponInfo['min_price'])?$couponInfo['min_price']:0;
|
|
|
+ $reducePrice = isset($couponInfo['reduce_price'])?$couponInfo['reduce_price']:0;
|
|
|
+
|
|
|
+ if($total<= $reducePrice){
|
|
|
+ $this->error = '金额不足优惠券使用条件';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 满足最低消费
|
|
|
+ if($reducePrice && $minPrice && $total >= $minPrice){
|
|
|
+ return ['total'=>moneyFormat($total-$reducePrice,2),'coupon_total'=>$reducePrice];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 购买券
|
|
|
+ else if($couponType == 20){
|
|
|
+ $reducePrice = isset($couponInfo['reduce_price'])?$couponInfo['reduce_price']:0;
|
|
|
+ if($total<= $reducePrice){
|
|
|
+ $this->error = '金额不足优惠券使用条件';
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ['total'=>moneyFormat($total-$reducePrice,2),'coupon_total'=>$reducePrice];
|
|
|
+ }
|
|
|
+ // 折扣券
|
|
|
+ else if($couponType == 30){
|
|
|
+ $reducePrice = isset($couponInfo['reduce_price'])?$couponInfo['reduce_price']:0;
|
|
|
+ $discount = isset($couponInfo['discount'])?$couponInfo['discount']:0;
|
|
|
+ $payTotal = moneyFormat($total * $discount/10, 2);
|
|
|
+ return ['total'=>$payTotal,'coupon_total'=> moneyFormat($total-$payTotal,2)];
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 专区商品
|
|
|
* @param $type 专区类型:2-午夜限定,3-蜜友优选
|
|
|
* @return array|mixed
|