...
|
...
|
@@ -15,6 +15,36 @@ class Order extends Model |
|
|
protected $createTime = 'createtime';
|
|
|
protected $updateTime = 'updatetime';
|
|
|
|
|
|
|
|
|
public function getCreatetimeAttr($value)
|
|
|
{
|
|
|
return date('Y-m-d H:i:s',$value);
|
|
|
}
|
|
|
public function selectPageData($where,$page,$limit)
|
|
|
{
|
|
|
$total = $this
|
|
|
->where($where)
|
|
|
->count();
|
|
|
|
|
|
$orderList = $this
|
|
|
->where($where)
|
|
|
->field('id order_id,order_num,createtime,pay_total,status,type,team_group_status,team_group_id')
|
|
|
->order('createtime desc')
|
|
|
->page($page,$limit)
|
|
|
->select();
|
|
|
|
|
|
return ['total' => $total, 'list' => $orderList];
|
|
|
}
|
|
|
|
|
|
public function getInfo($where)
|
|
|
{
|
|
|
$data = $this->alias('o')
|
|
|
->join('fa_order_address oa', 'oa.order_id=o.id')
|
|
|
->where($where)
|
|
|
->field('o.createtime,o.order_num,o.status,o.postage_date,o.postage_time,o.pay_type,o.pay_total,o.goods_total,o.postage_total,o.discount_price,o.score,o.team_group_status,o.ticket_price,o.ticketcode_id,oa.name,oa.mobile,oa.address,oa.ch_province_name,oa.ch_city_name,oa.ch_county_name')
|
|
|
->find();
|
|
|
return $data;
|
|
|
}
|
|
|
/**
|
|
|
* 余额支付
|
|
|
* @param $order_id 订单id
|
...
|
...
|
@@ -49,6 +79,7 @@ class Order extends Model |
|
|
//获取以仓库分好的商品
|
|
|
$goodsDepotList = setArr($data);
|
|
|
$orderInfoData = [];
|
|
|
$oldOrderInfoIds = []; //获取创建订单时 order_info_id
|
|
|
foreach ($goodsDepotList as $k => $goodsDepot) {
|
|
|
$detailArr = reset($goodsDepot);
|
|
|
$detailData = [
|
...
|
...
|
@@ -62,8 +93,6 @@ class Order extends Model |
|
|
];
|
|
|
//添加订单子表
|
|
|
$detailId = $orderDetailModel->insertGetId($detailData);
|
|
|
$oldOrderInfoIds = []; //获取创建订单时 order_info_id
|
|
|
|
|
|
foreach ($goodsDepot as $key => $goodsDepotInfo) {
|
|
|
$where = [
|
|
|
'order_id' => $order_id,
|
...
|
...
|
@@ -72,7 +101,7 @@ class Order extends Model |
|
|
'type' => $order['type'] == 3 ? 3 : $goodsDepotInfo['type'],
|
|
|
];
|
|
|
$orderInfo = $orderInfoModel->where($where)->find();
|
|
|
$oldOrderInfoIds[] = $orderInfo['id'];
|
|
|
if (!in_array($orderInfo['id'],$oldOrderInfoIds)) $oldOrderInfoIds[] = $orderInfo['id'];
|
|
|
$orderInfoData[] = [
|
|
|
'pay_type' => $orderInfo['pay_type'], //余额支付
|
|
|
'order_id' => $order_id,
|
...
|
...
|
@@ -177,6 +206,158 @@ class Order extends Model |
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 微信支付回调
|
|
|
* @param $order_id 订单id
|
|
|
* @param $user_id 用户id
|
|
|
* @param $money 支付金额
|
|
|
* @return boolean
|
|
|
*/
|
|
|
public function WXPay($order_id, $user_id, $money, $goodsList)
|
|
|
{
|
|
|
$goodsModel = new \app\api\model\Goods();
|
|
|
$userScoreLogModel = new \app\api\model\UserScoreLog();
|
|
|
$orderDetailModel = new \app\api\model\OrderDetail();
|
|
|
$orderInfoModel = new \app\api\model\OrderInfo();
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
$areaId = Db::name('order_address')->where('order_id', $order_id)->value('province_id');
|
|
|
$oldscore = Db::name('user')->where('id', $user_id)->value('score');
|
|
|
$integral_total = 0; //购买商品所需积分
|
|
|
//获取订单
|
|
|
$order = $this->where('id', $order_id)->field('id,pay_total,postage_total,ticket_price,ticketcode_price,type,team_group_id,team_group_status')->find();
|
|
|
|
|
|
foreach ($goodsList as $key => $goods) {
|
|
|
$integral_total += $goods['score']; //累计积分总数
|
|
|
if ($goods['type'] == 3) $goods['type'] = '1';
|
|
|
//根据仓库进行拆单
|
|
|
$data[] = $goodsModel->getDepot($goods['goods_id'], $areaId, $goods['number'], $goods['type']);
|
|
|
if (!$data[$key]) return ['code' => -1, 'msg' => $goods['ch_goods_name'] . ' 库存不足'];
|
|
|
}
|
|
|
//获取以仓库分好的商品
|
|
|
$goodsDepotList = setArr($data);
|
|
|
$orderInfoData = [];
|
|
|
$oldOrderInfoIds = []; //获取创建订单时 order_info_id
|
|
|
foreach ($goodsDepotList as $k => $goodsDepot) {
|
|
|
$detailArr = reset($goodsDepot);
|
|
|
$detailData = [
|
|
|
'order_id' => $order_id,
|
|
|
'user_id' => $user_id,
|
|
|
'pay_type' => 2, //余额支付
|
|
|
'depot_id' => $detailArr['depots_id'],
|
|
|
'status' => 2, //代发货
|
|
|
'createtime' => time(),
|
|
|
'updatetime' => time(),
|
|
|
];
|
|
|
//添加订单子表
|
|
|
$detailId = $orderDetailModel->insertGetId($detailData);
|
|
|
foreach ($goodsDepot as $key => $goodsDepotInfo) {
|
|
|
$where = [
|
|
|
'order_id' => $order_id,
|
|
|
'user_id' => $user_id,
|
|
|
'goods_id' => $goodsDepotInfo['goods_id'],
|
|
|
'type' => $order['type'] == 3 ? 3 : $goodsDepotInfo['type'],
|
|
|
];
|
|
|
$orderInfo = $orderInfoModel->where($where)->find();
|
|
|
if (!in_array($orderInfo['id'],$oldOrderInfoIds)) $oldOrderInfoIds[] = $orderInfo['id'];
|
|
|
$orderInfoData[] = [
|
|
|
'pay_type' => $orderInfo['pay_type'], //余额支付
|
|
|
'order_id' => $order_id,
|
|
|
'order_detail_id' => $detailId,
|
|
|
'user_id' => $user_id,
|
|
|
'goods_id' => $goodsDepotInfo['goods_id'],
|
|
|
'type' => $goodsDepotInfo['type'],
|
|
|
'ch_goods_name' => $orderInfo['ch_goods_name'],
|
|
|
'en_goods_name' => $orderInfo['en_goods_name'],
|
|
|
'goods_image' => $orderInfo['goods_image'],
|
|
|
'number' => $goodsDepotInfo['number'],
|
|
|
'goods_total' => round($orderInfo['goods_total'] / $orderInfo['number'] * $goodsDepotInfo['number'], 2),
|
|
|
'pay_total' => $orderInfo['pay_total'],
|
|
|
'postage_total' => $orderInfo['postage_total'],
|
|
|
'score' => $orderInfo['score'] / $orderInfo['number'] * $goodsDepotInfo['number'],
|
|
|
];
|
|
|
|
|
|
//减仓库库存
|
|
|
Db::name('depot')->where('id', $goodsDepotInfo['depots_id'])->setDec('stock_num', $goodsDepotInfo['number']);
|
|
|
//判断商品类型减去库存 加销量
|
|
|
if ($goodsDepotInfo['type'] == 2) {
|
|
|
Db::name('integral_goods')->where('id', $goodsDepotInfo['goods_id'])->setDec('stock_num', $goodsDepotInfo['number']);
|
|
|
Db::name('integral_goods')->where('id', $goodsDepotInfo['goods_id'])->setInc('sales_actual', $goodsDepotInfo['number']);
|
|
|
} else {
|
|
|
Db::name('goods')->where('id', $goodsDepotInfo['goods_id'])->setDec('stock_num', $goodsDepotInfo['number']);
|
|
|
Db::name('goods')->where('id', $goodsDepotInfo['goods_id'])->setInc('sales_actual', $goodsDepotInfo['number']);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//添加订单详情
|
|
|
$orderInfoModel->saveAll($orderInfoData);
|
|
|
//删除之前订单详情
|
|
|
$orderInfoModel->where(['id' => ['in', $oldOrderInfoIds]])->delete();
|
|
|
|
|
|
//有积分商品减积分
|
|
|
if ($integral_total != 0) {
|
|
|
$userScoreLogModel->setDecScore($user_id, $integral_total);
|
|
|
}
|
|
|
//将订单状态修改为已支付
|
|
|
$this->where('id', $order_id)->update(['status' => 2, 'paytime' => time()]);
|
|
|
//积分变动表记录
|
|
|
$oldscore = Db::name('user')->where('id', $user_id)->value('score');
|
|
|
$scoredata = [
|
|
|
'user_id' => $user_id,
|
|
|
'type' => 1, //购物返积分
|
|
|
'score' => intval($money),
|
|
|
'before' => $oldscore,
|
|
|
'after' => $oldscore + intval($money),
|
|
|
'createtime' => time(),
|
|
|
];
|
|
|
Db::name('user_score_log')->insert($scoredata);
|
|
|
Db::name('user')->where('id', $user_id)->setInc('score', intval($money));
|
|
|
|
|
|
|
|
|
$user_pid = Db::name('user')->where('id', $user_id)->value('pid');
|
|
|
//判断是否有上级进行返利 团购订单不在这里返利
|
|
|
if ($user_pid && $order['type'] != 3) {
|
|
|
if (!empty($order['ticket_price'])) $order['pay_total'] += $order['ticket_price'];
|
|
|
if (!empty($order['ticketcode_price'])) $order['pay_total'] += $order['ticketcode_price'];
|
|
|
if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
|
|
|
$parentInfo = Db::name('user')->where('id', $user_pid)->find();
|
|
|
//普通用户无返利
|
|
|
if ($parentInfo['type'] != 1) {
|
|
|
$rebateMoney = get_rebate($order['pay_total'], $parentInfo['type']);
|
|
|
$rebatedata = [
|
|
|
'user_id' => $user_pid,
|
|
|
'type' => 4, //佣金收入(返利)
|
|
|
'money' => $rebateMoney,
|
|
|
'before' => $parentInfo['money'],
|
|
|
'after' => $parentInfo['money'] + $rebateMoney,
|
|
|
'createtime' => time(),
|
|
|
];
|
|
|
Db::name('user_money_log')->insert($rebatedata);
|
|
|
Db::name('user')->where('id', $user_pid)->setInc('money', $rebateMoney);
|
|
|
}
|
|
|
}
|
|
|
if ($order['type'] == 3 && !empty($order['team_group_id'])) {
|
|
|
//参团
|
|
|
$this->joinTeam($order, $user_id);
|
|
|
} elseif ($order['type'] == 3) {
|
|
|
//开团
|
|
|
$this->createTeam($order, $user_id);
|
|
|
}
|
|
|
Db::commit();
|
|
|
return true;
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
Db::rollback();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 开团
|
|
|
* @param $order
|
|
|
* @param $user_id
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function createTeam($order, $user_id)
|
|
|
{
|
|
|
$goods_id = Db::name('order_info')->where(['order_id' => $order['id']])->value('goods_id');
|
...
|
...
|
@@ -237,6 +418,12 @@ class Order extends Model |
|
|
Db::name('team_group_info')->insertGetId($teamInfoData);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 参团
|
|
|
* @param $order
|
|
|
* @param $user_id
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function joinTeam($order, $user_id)
|
|
|
{
|
|
|
$user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();
|
...
|
...
|
@@ -292,8 +479,14 @@ class Order extends Model |
|
|
$this->where(['team_group_id' => $order['team_group_id'],'user_id'=>['not in',$ids]])->update(['team_group_status' => 3]);
|
|
|
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消订单
|
|
|
* @param $order
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function orderCancel($order){
|
|
|
$userTicketModel = new UserTicket();
|
|
|
$ticketcodeModel = new Ticketcode();
|
...
|
...
|
@@ -315,4 +508,13 @@ class Order extends Model |
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 订单退款
|
|
|
* @param $order
|
|
|
*/
|
|
|
public function orderRefund($order){
|
|
|
$this->where('id',$order['id'])->update(['status'=>4]);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|