...
|
...
|
@@ -104,7 +104,7 @@ class Order extends Api |
|
|
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $goodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
|
|
|
if (!$areaStock) $data['is_lack_stock'] = 1;
|
|
|
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
@@ -118,12 +118,12 @@ class Order extends Api |
|
|
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
|
|
|
if (!$areaStock) $data['is_lack_stock'] = 1;
|
|
|
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
|
|
|
}
|
|
|
}
|
|
|
//判断优惠码是否可用
|
|
|
if (!empty($ticketCode)) {
|
|
|
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->field('user_id,validtime')->find();
|
|
|
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->find();
|
|
|
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
|
|
|
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
|
|
|
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
|
...
|
...
|
@@ -268,7 +268,7 @@ class Order extends Api |
|
|
if (!$validate->check($param)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
if ($param['pay_type'] == 3 || $param['pay_type'] == 4) $this->error('员工或代理不能使用余额支付!');
|
|
|
if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
|
|
|
//判断优惠券和优惠码不能同时使用
|
|
|
if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
|
|
|
//获取普通商品id
|
...
|
...
|
@@ -383,11 +383,11 @@ class Order extends Api |
|
|
'pay_total' => $data['price'],
|
|
|
'discount_price' => $data['discount_price'],
|
|
|
'score' => $data['integral'],
|
|
|
'present_id' => !empty($data['present_id']) ? $data['present_id'] : '',
|
|
|
'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
|
|
|
'postage_total' => $postage,
|
|
|
'postage_type' => $param['postage_type'],
|
|
|
'postage_date' => $param['postage_date'],
|
|
|
'postage_time' => $param['postage_time'],
|
|
|
'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
|
|
|
'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
|
|
|
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
|
|
|
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
|
|
|
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
|
...
|
...
|
@@ -472,6 +472,384 @@ class Order extends Api |
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (确认订单(积分商品))
|
|
|
* @ApiSummary (确认订单(积分商品))
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
|
|
|
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
|
|
|
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
|
|
|
* @ApiParams (name=province_id, type=string, required=true, description="省id")
|
|
|
* @ApiParams (name=city_id, type=string, required=true, description="市")
|
|
|
* @ApiParams (name=county_id, type=string, required=true, description="县")
|
|
|
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
|
|
|
* @ApiRoute (/api/order/orderIntegral)
|
|
|
* @ApiReturn({
|
|
|
"code": 1,
|
|
|
"msg": "success",
|
|
|
"time": "1587691008",
|
|
|
"data": {
|
|
|
"price": 263.7, 总价
|
|
|
"discount_price": 12.3, 折扣价
|
|
|
"original_price": 246, 原价
|
|
|
"ticketcode_discount_price": 5, 优惠码优惠价
|
|
|
"ticket_discount_price": 10, 优惠券优惠价
|
|
|
"integral": 0, 积分
|
|
|
"postage": "30.00" 运费
|
|
|
}
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function orderIntegral()
|
|
|
{
|
|
|
$carModel = new \app\api\model\Car();
|
|
|
$goodsModel = new \app\api\model\Goods();
|
|
|
$ticketCodeModel = new \app\api\model\Ticketcode();
|
|
|
$userTicketModel = new \app\api\model\UserTicket();
|
|
|
$userId = $this->getUserId();
|
|
|
$goodsId = $this->request->param('goods_id');
|
|
|
$ticketId = $this->request->param('ticket_user_id');
|
|
|
$ticketCode = $this->request->param('ticket_code');
|
|
|
$provinceId = $this->request->param('province_id');
|
|
|
$cityId = $this->request->param('city_id');
|
|
|
$countyId = $this->request->param('county_id');
|
|
|
$postageType = $this->request->param('postage_type');
|
|
|
if (empty($goodsId)) $this->error('缺少参数 goods_id!');
|
|
|
if (empty($provinceId)) $this->error('缺少参数 province_id!');
|
|
|
if (empty($cityId)) $this->error('缺少参数 city_id!');
|
|
|
if (empty($countyId)) $this->error('缺少参数 county_id!');
|
|
|
if (empty($postageType)) $this->error('缺少参数 postage_type!');
|
|
|
//判断优惠券和优惠码不能同时使用
|
|
|
if (!empty($ticketCode) && !empty($ticketId)) $this->error('优惠券与优惠码不能同时使用!');
|
|
|
|
|
|
|
|
|
//获取积分商品状态
|
|
|
$goodsInfo = $this->integralGoodsModel->where(['id' => $goodsId])->field('id,status,stock_num,goods_price,integral')->find();
|
|
|
|
|
|
//判断商品是否有库存和状态
|
|
|
if ($goodsInfo['status'] == 2) $this->error($goodsInfo['ch_name'] . '商品已下架');
|
|
|
if ($goodsInfo['stock_num'] == 0) $this->error($goodsInfo['ch_name'] . '商品库存不足');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $goodsInfo['id'], 'd.area_id' => $provinceId]);
|
|
|
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
|
|
|
|
|
|
//判断优惠码是否可用
|
|
|
if (!empty($ticketCode)) {
|
|
|
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->find();
|
|
|
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
|
|
|
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
|
|
|
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
|
|
|
}
|
|
|
|
|
|
//判断优惠券是否可用
|
|
|
if (!empty($ticketId)) {
|
|
|
$ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $ticketId, 'status' => 1])->find();
|
|
|
if (!$ticketInfo) $this->error('优惠码不存在!');
|
|
|
}
|
|
|
|
|
|
//获取原价,现价,折扣
|
|
|
$goodsInfo['user_type'] = $this->user['type'];
|
|
|
$data['price'] = round(get_price($goodsInfo), 2); //折扣后价格
|
|
|
$data['discount_price'] = round(get_discount_price($goodsInfo), 2);//折扣金额
|
|
|
$data['original_price'] = $goodsInfo['goods_price'];//原价
|
|
|
$data['integral'] = $goodsInfo['integral']; //积分
|
|
|
|
|
|
|
|
|
//判断积分
|
|
|
if ($data['integral'] > $this->user['score']) $this->error('积分不足');
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
|
|
|
//特殊地区满200免运费
|
|
|
if ($is_special == 1 && $data['price'] >= 200) {
|
|
|
$postage = 0;
|
|
|
//其他地区满300免运费
|
|
|
} elseif ($data['price'] >= 300) {
|
|
|
$postage = 0;
|
|
|
} else {
|
|
|
//都不满足获取对应的运费
|
|
|
$postageWhere = ['province_id' => $provinceId, 'city_id' => $cityId, 'county_id' => $countyId];
|
|
|
if ($postageType == 2) {
|
|
|
//配送为闪送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage2');
|
|
|
}
|
|
|
} else {
|
|
|
//配送为普通配送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage1');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$data['postage'] = $postage;
|
|
|
$data['price'] += $postage;
|
|
|
|
|
|
//使用优惠码
|
|
|
if (!empty($ticketCodeInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketCodeInfo['price'];
|
|
|
$data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
|
|
|
}
|
|
|
|
|
|
//使用优惠券
|
|
|
if (!empty($ticketInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketInfo['price'];
|
|
|
$data['ticket_discount_price'] = $ticketInfo['price'];
|
|
|
}
|
|
|
|
|
|
$this->success('success', $data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (创建订单(积分商品))
|
|
|
* @ApiSummary (创建订单(积分商品))
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
|
|
|
* @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
|
|
|
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
|
|
|
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
|
|
|
* @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
|
|
|
* @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
|
|
|
* @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
|
|
|
* @ApiParams (name=address, type=string, required=true, description="商品收货地址")
|
|
|
* @ApiParams (name=name, type=string, required=true, description="商品收货人")
|
|
|
* @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
|
|
|
* @ApiParams (name=present_id, type=string, required=false, description="赠品id")
|
|
|
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
|
|
|
* @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
|
|
|
* @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
|
|
|
* @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
|
|
|
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
|
|
|
* @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
|
|
|
* @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
|
|
|
* @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
|
|
|
* @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
|
|
|
* @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
|
|
|
* @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
|
|
|
* @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
|
|
|
* @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
|
|
|
* @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
|
|
|
* @ApiParams (name=order_note, type=string, required=false, description="订单备注")
|
|
|
* @ApiRoute (/api/order/createOrderIntegral)
|
|
|
* @ApiReturn({
|
|
|
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function createOrderIntegral()
|
|
|
{
|
|
|
$ticketCodeModel = new \app\api\model\Ticketcode();
|
|
|
$userTicketModel = new \app\api\model\UserTicket();
|
|
|
$userId = $this->getUserId();
|
|
|
$param = $this->request->param();
|
|
|
$param['user_id'] = $userId;
|
|
|
$validate = new Validate([
|
|
|
'goods_id' => 'require',
|
|
|
'pay_type' => 'require',
|
|
|
'name' => 'require',
|
|
|
'mobile' => 'require',
|
|
|
'address' => 'require',
|
|
|
'province_id' => 'require',
|
|
|
'city_id' => 'require',
|
|
|
'county_id' => 'require',
|
|
|
'postage_type' => 'require',
|
|
|
'invoice_status' => 'require',
|
|
|
|
|
|
]);
|
|
|
|
|
|
$validate->message([
|
|
|
'goods_id' => '缺少参数 goods_id!',
|
|
|
'pay_type' => '缺少参数 pay_type!',
|
|
|
'name' => '缺少参数 name!',
|
|
|
'mobile' => '缺少参数 mobile!',
|
|
|
'address' => '缺少参数 address!',
|
|
|
'province_id' => '缺少参数 province_id!',
|
|
|
'city_id' => '缺少参数 city_id!',
|
|
|
'county_id' => '缺少参数 county_id!',
|
|
|
'postage_type' => '缺少参数 postage_type!',
|
|
|
'invoice_status' => '缺少参数 invoice_status!',
|
|
|
]);
|
|
|
|
|
|
if (!$validate->check($param)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
|
|
|
//判断优惠券和优惠码不能同时使用
|
|
|
if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
|
|
|
//获取积分商品状态
|
|
|
$goodsInfo = $this->integralGoodsModel->where(['id' => $param['goods_id']])->field('id,ch_name,en_name,image,status,stock_num,goods_price,integral')->find();
|
|
|
|
|
|
//判断商品是否有库存和状态
|
|
|
if ($goodsInfo['status'] == 2) $this->error($goodsInfo['ch_name'] . '商品已下架');
|
|
|
if ($goodsInfo['stock_num'] == 0) $this->error($goodsInfo['ch_name'] . '商品库存不足');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $goodsInfo['id'], 'd.area_id' => $param['province_id']]);
|
|
|
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
|
|
|
|
|
|
//判断优惠码是否可用
|
|
|
if (!empty($param['ticket_code'])) {
|
|
|
$ticketCodeInfo = $ticketCodeModel->where('code', $param['ticket_code'])->find();
|
|
|
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
|
|
|
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
|
|
|
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
|
|
|
}
|
|
|
|
|
|
//判断优惠券是否可用
|
|
|
if (!empty($param['ticket_user_id'])) {
|
|
|
$ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $param['ticket_user_id'], 'status' => 1])->find();
|
|
|
if (!$ticketInfo) $this->error('优惠码不存在!');
|
|
|
}
|
|
|
|
|
|
//获取原价,现价,折扣
|
|
|
$goodsInfo['user_type'] = $this->user['type'];
|
|
|
$data['price'] = round(get_price($goodsInfo), 2); //折扣后价格
|
|
|
$data['discount_price'] = round(get_discount_price($goodsInfo), 2);//折扣金额
|
|
|
$data['original_price'] = $goodsInfo['goods_price'];//原价
|
|
|
$data['integral'] = $goodsInfo['integral']; //积分
|
|
|
|
|
|
//判断积分
|
|
|
if ($data['integral'] > $this->user['score']) $this->error('积分不足');
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
|
|
|
//特殊地区满200免运费
|
|
|
if ($is_special == 1 && $data['price'] >= 200) {
|
|
|
$postage = 0;
|
|
|
//其他地区满300免运费
|
|
|
} elseif ($data['price'] >= 300) {
|
|
|
$postage = 0;
|
|
|
} else {
|
|
|
//都不满足获取对应的运费
|
|
|
$postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
|
|
|
if ($param['postage_type'] == 2) {
|
|
|
//配送为闪送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
|
|
|
}
|
|
|
} else {
|
|
|
//配送为普通配送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$data['postage'] = $postage;
|
|
|
$data['price'] += $postage;
|
|
|
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
//使用优惠码
|
|
|
if (!empty($ticketCodeInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketCodeInfo['price'];
|
|
|
$data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
|
|
|
$data['ticketcode_id'] = $ticketCodeInfo['id'];
|
|
|
$this->ticketCodeModel->where('id', $ticketCodeInfo['id'])->update(['user_id' => $userId]);
|
|
|
}
|
|
|
|
|
|
//使用优惠券
|
|
|
if (!empty($ticketInfo)) {
|
|
|
$data['price'] = $data['price'] - $ticketInfo['price'];
|
|
|
$data['ticket_discount_price'] = $ticketInfo['price'];
|
|
|
$data['user_ticket_id'] = $ticketInfo['id'];
|
|
|
$this->userTicketModel->where('id', $ticketInfo['id'])->update(['status' => 2]);
|
|
|
|
|
|
}
|
|
|
|
|
|
//order表数据
|
|
|
$orderData = [
|
|
|
'order_num' => get_order_num(),
|
|
|
'user_id' => $userId,
|
|
|
'pay_type' => $param['pay_type'],
|
|
|
'goods_total' => $data['original_price'],
|
|
|
'pay_total' => $data['price'],
|
|
|
'discount_price' => $data['discount_price'],
|
|
|
'score' => $data['integral'],
|
|
|
'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
|
|
|
'postage_total' => $postage,
|
|
|
'postage_type' => $param['postage_type'],
|
|
|
'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
|
|
|
'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
|
|
|
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
|
|
|
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
|
|
|
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
|
|
|
'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
|
|
|
'invoice_status' => $param['invoice_status'],
|
|
|
'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
|
|
|
'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
|
|
|
'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
|
|
|
'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
|
|
|
'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
|
|
|
'type' => 2,
|
|
|
'status' => 1,
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res1 = $this->orderModel->insertGetId($orderData);
|
|
|
|
|
|
//收货地址数据
|
|
|
$orderAddress = [
|
|
|
'order_id' => $res1,
|
|
|
'name' => $param['name'],
|
|
|
'mobile' => $param['mobile'],
|
|
|
'address' => $param['address'],
|
|
|
'province_id' => $param['province_id'],
|
|
|
'city_id' => $param['city_id'],
|
|
|
'county_id' => $param['county_id'],
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res2 = $this->orderAddressModel->insertData($orderAddress);
|
|
|
if ($param['invoice_status'] != 3) {
|
|
|
//收货地址数据
|
|
|
$orderAddressInvoice = [
|
|
|
'order_id' => $res1,
|
|
|
'name' => $param['invoice_name'],
|
|
|
'mobile' => $param['invoice_mobile'],
|
|
|
'address' => $param['invoice_address'],
|
|
|
'province_id' => $param['invoice_province_id'],
|
|
|
'city_id' => $param['invoice_city_id'],
|
|
|
'county_id' => $param['invoice_county_id'],
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
|
|
|
}
|
|
|
|
|
|
//获取商品详情列表
|
|
|
$orderInfoData = [
|
|
|
'order_id' => $res1,
|
|
|
'pay_type' => $param['pay_type'],
|
|
|
'user_id' => $userId,
|
|
|
'goods_id' => $goodsInfo['id'],
|
|
|
'ch_goods_name' => $goodsInfo['ch_name'],
|
|
|
'en_goods_name' => $goodsInfo['en_name'],
|
|
|
'goods_image' => $goodsInfo['image'],
|
|
|
'number' => 1,
|
|
|
'type' => 2, //积分商品
|
|
|
'score' => !empty($data['integral']) ? $data['integral'] : 0,
|
|
|
'goods_total' => get_price($goodsInfo),
|
|
|
'pay_total' => $data['price'],
|
|
|
'postage_total' => $postage,
|
|
|
];
|
|
|
|
|
|
$res4 = $this->orderInfoModel->save($orderInfoData);
|
|
|
|
|
|
if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
|
|
|
Db::commit();
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
Db::rollback();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (确认订单(团购商品))
|
...
|
...
|
@@ -479,7 +857,6 @@ class Order extends Api |
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
|
|
|
* @ApiParams (name=number, type=string, required=true, description="商品数量")
|
|
|
* @ApiParams (name=province_id, type=string, required=true, description="省id")
|
|
|
* @ApiParams (name=city_id, type=string, required=true, description="市")
|
|
|
* @ApiParams (name=county_id, type=string, required=true, description="县")
|
...
|
...
|
@@ -502,43 +879,36 @@ class Order extends Api |
|
|
*/
|
|
|
public function orderTeam()
|
|
|
{
|
|
|
$goodsModel = new \app\api\model\Goods();
|
|
|
$userId = $this->getUserId();
|
|
|
$number = $this->request->param('number');
|
|
|
$goodsId = $this->request->param('goods_id');
|
|
|
$provinceId = $this->request->param('province_id');
|
|
|
$cityId = $this->request->param('city_id');
|
|
|
$countyId = $this->request->param('county_id');
|
|
|
$postageType = $this->request->param('postage_type');
|
|
|
if (empty($goodsId)) $this->error('缺少参数 goods_id!');
|
|
|
if (empty($number)) $this->error('缺少参数 number!');
|
|
|
if (empty($provinceId)) $this->error('缺少参数 province_id!');
|
|
|
if (empty($cityId)) $this->error('缺少参数 city_id!');
|
|
|
if (empty($countyId)) $this->error('缺少参数 county_id!');
|
|
|
if (empty($postageType)) $this->error('缺少参数 postage_type!');
|
|
|
|
|
|
$goodsInfo = $goodsModel->where(['id' => $goodsId])->field('id,status,stock_num')->find();
|
|
|
$goodsInfo = $this->goodsModel->where(['id' => $goodsId])->field('id,status,stock_num,is_group,group_num,group_price,grouptime,goods_price')->find();
|
|
|
//判断商品是否有库存和状态
|
|
|
if ($goodsInfo['status'] == 2) $this->error('商品已下架');
|
|
|
if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
|
|
|
if ($goodsInfo['is_group'] != 1) $this->error('该商品不是团购商品');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);
|
|
|
if (!$areaStock) $data['is_lack_stock'] = 1;
|
|
|
$areaStock = $this->goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);
|
|
|
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
|
|
|
|
|
|
//获取原价,现价,折扣
|
|
|
$priceArr = $this->carModel->getCarMoney(['id' => ['in', $carId]]);
|
|
|
$data['price'] = 0; //折扣后价格
|
|
|
$data['discount_price'] = 0; //折扣金额
|
|
|
$data['original_price'] = 0; //原价
|
|
|
$data['integral'] = 0; //积分
|
|
|
foreach ($priceArr as $k => $v) {
|
|
|
$priceArr[$k]['user_type'] = $this->user['type'];
|
|
|
$data['price'] += round(get_price($v), 2);
|
|
|
$data['discount_price'] += round(get_discount_price($v), 2);
|
|
|
$data['original_price'] += $v['goods_price'];
|
|
|
if (!empty($v['integral'])) $data['integral'] += $v['integral'];
|
|
|
$goodsInfo['user_type'] = $this->user['type'];
|
|
|
$data['price'] += round(get_price($goodsInfo), 2);
|
|
|
$data['discount_price'] += round(get_discount_price($goodsInfo), 2);
|
|
|
$data['original_price'] += $goodsInfo['group_price'];
|
|
|
|
|
|
}
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
|
|
|
//特殊地区满200免运费
|
...
|
...
|
@@ -572,6 +942,229 @@ class Order extends Api |
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (创建订单(团购商品))
|
|
|
* @ApiSummary (创建订单(团购商品))
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
|
|
|
* @ApiParams (name=goods_id, type=string, required=false, description="商品id")
|
|
|
* @ApiParams (name=team_group_id, type=string, required=false, description="拼团id")
|
|
|
* @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
|
|
|
* @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
|
|
|
* @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
|
|
|
* @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
|
|
|
* @ApiParams (name=address, type=string, required=true, description="商品收货地址")
|
|
|
* @ApiParams (name=name, type=string, required=true, description="商品收货人")
|
|
|
* @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
|
|
|
* @ApiParams (name=present_id, type=string, required=false, description="赠品id")
|
|
|
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
|
|
|
* @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
|
|
|
* @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
|
|
|
* @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
|
|
|
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
|
|
|
* @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
|
|
|
* @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
|
|
|
* @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
|
|
|
* @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
|
|
|
* @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
|
|
|
* @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
|
|
|
* @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
|
|
|
* @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
|
|
|
* @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
|
|
|
* @ApiParams (name=order_note, type=string, required=false, description="订单备注")
|
|
|
* @ApiRoute (/api/order/createOrderTeam)
|
|
|
* @ApiReturn({
|
|
|
|
|
|
},
|
|
|
})
|
|
|
*/
|
|
|
public function createOrderTeam()
|
|
|
{
|
|
|
$userId = $this->getUserId();
|
|
|
$param = $this->request->param();
|
|
|
$param['user_id'] = $userId;
|
|
|
$validate = new Validate([
|
|
|
'goods_id' => 'require',
|
|
|
'pay_type' => 'require',
|
|
|
'name' => 'require',
|
|
|
'mobile' => 'require',
|
|
|
'address' => 'require',
|
|
|
'province_id' => 'require',
|
|
|
'city_id' => 'require',
|
|
|
'county_id' => 'require',
|
|
|
'postage_type' => 'require',
|
|
|
'invoice_status' => 'require',
|
|
|
|
|
|
]);
|
|
|
|
|
|
$validate->message([
|
|
|
'goods_id' => '缺少参数 goods_id!',
|
|
|
'pay_type' => '缺少参数 pay_type!',
|
|
|
'name' => '缺少参数 name!',
|
|
|
'mobile' => '缺少参数 mobile!',
|
|
|
'address' => '缺少参数 address!',
|
|
|
'province_id' => '缺少参数 province_id!',
|
|
|
'city_id' => '缺少参数 city_id!',
|
|
|
'county_id' => '缺少参数 county_id!',
|
|
|
'postage_type' => '缺少参数 postage_type!',
|
|
|
'invoice_status' => '缺少参数 invoice_status!',
|
|
|
]);
|
|
|
|
|
|
if (!$validate->check($param)) {
|
|
|
$this->error($validate->getError());
|
|
|
}
|
|
|
if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
|
|
|
|
|
|
$goodsInfo = $this->goodsModel->where(['id' => $param['goods_id']])->field('id,status,stock_num,is_group,group_num,group_price,grouptime,goods_price,ch_name,en_name,image')->find();
|
|
|
//判断商品是否有库存和状态
|
|
|
if ($goodsInfo['status'] == 2) $this->error('商品已下架');
|
|
|
if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
|
|
|
if ($goodsInfo['is_group'] != 1) $this->error('该商品不是团购商品');
|
|
|
//判断用户收货区域是否有库存
|
|
|
$areaStock = $this->goodsModel->getAreaStockNum(['g.id' => $param['goods_id'], 'd.area_id' => $param['province_id']]);
|
|
|
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
|
|
|
|
|
|
//判断团购是否满人
|
|
|
if (!empty($param['team_group_id'])) {
|
|
|
$teamInfo = Db::name('team_group')->where('id', $param['team_group_id'])->field('group_num,grouptime')->find();
|
|
|
$count = Db::name('team_group_info')->where('team_group_id', $param['team_group_id'])->count();
|
|
|
$is_join_team = Db::name('team_group_info')->where(['team_group_id' => $param['team_group_id'], 'user_id' => $userId])->count();
|
|
|
if (!$teamInfo) $this->error('没有此拼团信息');
|
|
|
if ($teamInfo['grouptime'] <= time()) $this->error('拼团已失效');
|
|
|
if ($teamInfo['group_num'] <= $count) $this->error('参团人数已满');
|
|
|
if ($is_join_team) $this->error('您已参团了');
|
|
|
}
|
|
|
|
|
|
//获取原价,现价,折扣
|
|
|
$data['price'] = 0; //折扣后价格
|
|
|
$data['discount_price'] = 0; //折扣金额
|
|
|
$data['original_price'] = 0; //原价
|
|
|
$goodsInfo['user_type'] = $this->user['type'];
|
|
|
$data['price'] += round(get_price($goodsInfo), 2);
|
|
|
$data['discount_price'] += round(get_discount_price($goodsInfo), 2);
|
|
|
$data['original_price'] += $goodsInfo['group_price'];
|
|
|
|
|
|
//获取运费
|
|
|
$is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
|
|
|
//特殊地区满200免运费
|
|
|
if ($is_special == 1 && $data['price'] >= 200) {
|
|
|
$postage = 0;
|
|
|
//其他地区满300免运费
|
|
|
} elseif ($data['price'] >= 300) {
|
|
|
$postage = 0;
|
|
|
} else {
|
|
|
//都不满足获取对应的运费
|
|
|
$postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
|
|
|
if ($param['postage_type'] == 2) {
|
|
|
//配送为闪送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
|
|
|
}
|
|
|
} else {
|
|
|
//配送为普通配送
|
|
|
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
|
|
|
if (!$postage) {
|
|
|
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$data['postage'] = $postage;
|
|
|
$data['price'] += $postage;
|
|
|
|
|
|
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
//order表数据
|
|
|
$orderData = [
|
|
|
'order_num' => get_order_num(),
|
|
|
'user_id' => $userId,
|
|
|
'pay_type' => $param['pay_type'],
|
|
|
'goods_total' => $data['original_price'],
|
|
|
'pay_total' => $data['price'],
|
|
|
'discount_price' => $data['discount_price'],
|
|
|
'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
|
|
|
'team_group_id' => !empty($param['team_group_id']) ? $param['team_group_id'] : '',
|
|
|
'team_group_status' => 1,
|
|
|
'postage_total' => $postage,
|
|
|
'postage_type' => $param['postage_type'],
|
|
|
'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
|
|
|
'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
|
|
|
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
|
|
|
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
|
|
|
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
|
|
|
'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
|
|
|
'invoice_status' => $param['invoice_status'],
|
|
|
'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
|
|
|
'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
|
|
|
'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
|
|
|
'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
|
|
|
'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
|
|
|
'type' => 3,
|
|
|
'status' => 1,
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res1 = $this->orderModel->insertGetId($orderData);
|
|
|
|
|
|
//收货地址数据
|
|
|
$orderAddress = [
|
|
|
'order_id' => $res1,
|
|
|
'name' => $param['name'],
|
|
|
'mobile' => $param['mobile'],
|
|
|
'address' => $param['address'],
|
|
|
'province_id' => $param['province_id'],
|
|
|
'city_id' => $param['city_id'],
|
|
|
'county_id' => $param['county_id'],
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res2 = $this->orderAddressModel->insertData($orderAddress);
|
|
|
if ($param['invoice_status'] != 3) {
|
|
|
//收货地址数据
|
|
|
$orderAddressInvoice = [
|
|
|
'order_id' => $res1,
|
|
|
'name' => $param['invoice_name'],
|
|
|
'mobile' => $param['invoice_mobile'],
|
|
|
'address' => $param['invoice_address'],
|
|
|
'province_id' => $param['invoice_province_id'],
|
|
|
'city_id' => $param['invoice_city_id'],
|
|
|
'county_id' => $param['invoice_county_id'],
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
|
|
|
}
|
|
|
|
|
|
//获取商品详情
|
|
|
$orderInfoData = [
|
|
|
'order_id' => $res1,
|
|
|
'pay_type' => $param['pay_type'],
|
|
|
'user_id' => $userId,
|
|
|
'goods_id' => $param['goods_id'],
|
|
|
'ch_goods_name' => $goodsInfo['ch_name'],
|
|
|
'en_goods_name' => $goodsInfo['en_name'],
|
|
|
'goods_image' => $goodsInfo['image'],
|
|
|
'number' => 1, //团购商品购买数量只能是1
|
|
|
'type' => 3,
|
|
|
'goods_total' => $data['original_price'],
|
|
|
'pay_total' => $data['price'],
|
|
|
'postage_total' => $postage,
|
|
|
];
|
|
|
|
|
|
$res4 = $this->orderInfoModel->save($orderInfoData);
|
|
|
|
|
|
if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
|
|
|
Db::commit();
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
Db::rollback();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (订单付款)
|
...
|
...
|
@@ -601,6 +1194,7 @@ class Order extends Api |
|
|
$this->error(__($validate->getError()));
|
|
|
}
|
|
|
$order = $this->orderModel->where(['id' => $param['order_id']])->find();
|
|
|
if (!$order) $this->error('订单不存在');
|
|
|
if ($order['status'] == 2) $this->error('订单已支付');
|
|
|
if ($order['status'] == -1) $this->error('订单已取消');
|
|
|
//判断积分
|
...
|
...
|
@@ -618,6 +1212,13 @@ class Order extends Api |
|
|
if (!$is_stock) $this->error($v['ch_goods_name'] . ' 库存不足');
|
|
|
}
|
|
|
}
|
|
|
//判断团购是否满人
|
|
|
if (!empty($order['team_group_id'])) {
|
|
|
$teamInfo = Db::name('team_group')->where('id', $order['team_group_id'])->field('group_num,grouptime')->find();
|
|
|
$count = Db::name('team_group_info')->where('team_group_id', $order['team_group_id'])->count();
|
|
|
if ($teamInfo['group_num'] <= $count) $this->error('参团人数已满');
|
|
|
if ($teamInfo['grouptime'] <= time()) $this->error('拼团已失效');
|
|
|
}
|
|
|
$amount = $order['pay_total'];
|
|
|
$payType = $order['pay_type'];
|
|
|
if ($payType == 2) {
|
...
|
...
|
@@ -634,4 +1235,41 @@ class Order extends Api |
|
|
//微信支付
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @ApiTitle (取消订单)
|
|
|
* @ApiSummary (取消订单)
|
|
|
* @ApiMethod (POST)
|
|
|
* @ApiRoute (/api/order/orderCancel)
|
|
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
* @ApiParams (name=order_id, type=string, required=true, description="订单id")
|
|
|
* @ApiReturn ({
|
|
|
})
|
|
|
*/
|
|
|
public function orderCancel()
|
|
|
{
|
|
|
$userId = $this->getUserId();
|
|
|
$param = $this->request->param();
|
|
|
$rule = [
|
|
|
'order_id' => 'require',
|
|
|
];
|
|
|
|
|
|
$msg = [
|
|
|
'order_id' => '缺少参数 order_id!',
|
|
|
];
|
|
|
|
|
|
$validate = new Validate($rule, $msg);
|
|
|
$result = $validate->check($param);
|
|
|
if (!$result) {
|
|
|
$this->error(__($validate->getError()));
|
|
|
}
|
|
|
$order = $this->orderModel->where(['id' => $param['order_id']])->find();
|
|
|
if (!$order) $this->error('订单不存在');
|
|
|
if ($order['status'] == 2) $this->error('订单已支付');
|
|
|
if ($order['status'] == -1) $this->error('订单已取消');
|
|
|
|
|
|
$res = $this->orderModel->orderCancel($order);
|
|
|
if ($res) $this->success('取消订单成功');
|
|
|
else $this->error('取消订单失败');
|
|
|
}
|
|
|
} |
...
|
...
|
|