作者 何书鹏

优惠券使用逻辑修改

... ... @@ -502,7 +502,8 @@ class Order extends Api
'discount_price' => $couponprice, // 优惠价格
'pay_price' => bcadd($sum_price,$couponprice,2), // 支付总价
'express_price' => $distance_price, // 骑手费用
'user_id' => $user_id
'user_id' => $user_id,
'user_coupon_id' => $coupon_id > 0 ? $coupon_id : 0
];
// 添加订单
... ... @@ -530,6 +531,8 @@ class Order extends Api
->whereIn('sku_id',$skuids)
->where('user_id',$this->auth->id)
->delete();
// 如果使用了优惠券,就把优惠券状态变为已使用
UserCoupon::where('id',$coupon_id)->update(['status'=>'1']);
Db::commit();
}catch (PDOException $e){
Db::rollback();
... ... @@ -653,37 +656,55 @@ class Order extends Api
if ($order['freight_status'] == 20) $this->error('订单已发货无法取消');
$goodsmodel = new \app\api\model\OrderGoods();
$skumodel = new \app\api\model\GoodsSpec();
// 增加库存
$list = $goodsmodel->where('order_id',$order['id'])->select();
foreach ($list as $key => $value){
if ($value['deduct_stock_type'] == 10){
$skumodel->where('goods_spec_id',$value['goods_spec_id'])->setInc('stock_num',$value['total_num']);
Db::startTrans();
try {
// 增加库存
$list = $goodsmodel->where('order_id',$order['id'])->select();
foreach ($list as $key => $value){
if ($value['deduct_stock_type'] == 10){
$skumodel->where('goods_spec_id',$value['goods_spec_id'])->setInc('stock_num',$value['total_num']);
}
}
}
// 已支付就退款
if($order['pay_status'] == '20'){
$config = Service::getConfig('wechat');
$config['app_id'] = $config['miniapp_id'];
$config['notify_url'] = $this->request->domain().'api/notify/refund/type/wechat';
$config['return_url'] = '';
$wechat = Pay::wechat($config);
$refund_no = 'LQ-'.time().mt_rand(10000,99999);
$param = [
// 已支付就退款
if($order['pay_status'] == '20'){
$config = Service::getConfig('wechat');
$config['app_id'] = $config['miniapp_id'];
$config['notify_url'] = $this->request->domain().'api/notify/refund/type/wechat';
$config['return_url'] = '';
$wechat = Pay::wechat($config);
$refund_no = 'LQ-'.time().mt_rand(10000,99999);
$param = [
// 'total_fee' => $order['pay_price'],
// 'refund_fee' => $order['pay_price'],
'total_fee' => 1,
'refund_fee' => 1,
'out_trade_no' => $order['order_no'],
'out_refund_no' => $refund_no
];
$wechat->refund($param);
$order->refund_no = $refund_no;
$order->refund_time = time();
'total_fee' => 1,
'refund_fee' => 1,
'out_trade_no' => $order['order_no'],
'out_refund_no' => $refund_no
];
$wechat->refund($param);
$order->refund_no = $refund_no;
$order->refund_time = time();
}
// 状态变为已取消
$order->order_status = '20';
$order->status = 'hidden';
$order->isUpdate(true)->save();
// 如果使用了优惠券,就把优惠券状态变为待使用或者已过期
if($order['user_coupon_id'] > 0){
$user_coupon = UserCoupon::get($order['user_coupon_id']);
if($user_coupon){
$status = $user_coupon['endtime'] > time() ? '0' : '2';
$user_coupon->save(['status'=>$status]);
}
}
Db::commit();
}catch (PDOException $e){
Db::rollback();
$this->error($e->getMessage());
}
// 状态变为已取消
$order->order_status = '20';
$order->status = 'hidden';
$order->isUpdate(true)->save();
$this->success('取消成功');
}
... ...