[1自提 2配送 3快递],all_money=>总价,address_id=>收货地址id, * coupon_id=>优惠券id,coupon_type=>优惠券类型,link_name=>姓名,mobile_phone=>电话, * delivery_fee=>配送费,remark=>用户留言,start_date=>配送或者自提时间, * advance_id=>预售活动,goods_id=>商品id,sku_id=>规格id,num=>购买数量] */ public function advance_place_order() { //验证参数列表及规则 $verifyItem = array( 'pickup_type' => array('in' => array(1, 2, 3), 'require_link' => array( array('require_data' => 1, 'link_key' => array('link_name', 'mobile_phone'))), array('require_data' => 2, 'link_key' => array('address_id')), array('require_data' => 3, 'link_key' => array('address_id')), ), 'all_money' => array('type' => 'money'), 'goods_id' => array('type' => 'number'), 'sku_id' => array('type' => 'number', 'require' => false), 'num' => array('type' => 'number'), 'address_id' => array('type' => 'number', 'min' => 1, 'require' => false), 'coupon_id' => array('type' => 'number', 'min' => 1, 'require' => false), 'coupon_type' => array('in' => array(1, 2), 'require' => false), 'link_name' => array('type' => 'name', 'require' => false), 'mobile_phone' => array('type' => 'mobile', 'require' => false), 'delivery_fee' => array('type' => 'money', 'min' => 0, 'require' => false), 'remark' => array('type' => 'string', 'maxLen' => 255, 'require' => false), 'start_date' => array('type' => 'string', 'require' => false), ); $post = I('post.'); $verify = verifyRequestParam($post, $verifyItem);//验证参数是否正确 if ($verify['error'] == 1) { $result['status'] = 0; $result['msg'] = $verify['key'] . $verify['message']; $this->ajaxReturn($result); } $pickup_type = $post['pickup_type']; $frontAllMoney = $post['all_money']; $address_id = $post['address_id']; $coupon_id = $post['coupon_id']; $coupon_type = $post['coupon_type']; $link_name = $post['link_name']; $mobile_phone = $post['mobile_phone']; $delivery_fee = $post['delivery_fee']; $remark = $post['remark']; $user_select_time = $post['start_date']; $user_select_time = strtotime($user_select_time); $advance_id = $post['advance_id']; $goods_id = $post['goods_id']; $sku_id = $post['sku_id']; $num = $post['num']; $uid = self::$uid; $store_id = self::$store_id; $merchant_id = self::$merchant_id; $time = time(); $ver = $this->verifyPlaceOrder(); if (!$ver) { $this->ajaxReturn(array('status' => 0, 'msg' => '当前时间未在商家营业时间内')); } $AdvanceGoods = new AdvanceGoodsModel(); $advance_sale_status = $AdvanceGoods->advance_master_verify($store_id); //预售活动总开关验证 if ($advance_sale_status['status'] == 0) { $this->ajaxReturn($advance_sale_status); } $storeSet = $this->getStoreSet_v2();//获取门店设置 $delivery_set = $storeSet['delivery_set'];//存储是否开启自提、同城配送、快递发货 if ($delivery_set[$pickup_type - 1] != 1) { $res = array('status' => 0, 'msg' => '该配送方式已关闭'); $this->ajaxReturn($res); } $advance_info = $AdvanceGoods->advance_activity_state(array("merchant_id" => $merchant_id, "store_id" => $store_id, "advance_id" => $advance_id, "pickup_type" => $pickup_type)); //验证预售活动 if ($advance_info['status'] == 0) { $this->ajaxReturn($advance_info); } $advance_info = $advance_info['advance']; $goods = array( 'goods_id' => $goods_id, 'sku_id' => $sku_id, 'num' => $num, 'delivery_template_id' => $advance_info['delivery_template_id'], 'weight_value' => $advance_info['weight_value'], ); $advance_goods_info = $AdvanceGoods->advance_goods_state(array("goods" => $goods, "advance_info" => $advance_info, 'uid' => $uid)); //验证预售商品 if ($advance_goods_info['status'] == 0) { $this->ajaxReturn($advance_goods_info); } $BillModel = D('RetailBillOrder');//订单主表 $SkuListModel = D('RetailSkuList'); $SellModel = D('RetailSell'); switch ($advance_info['attribute_state']) { case 2: $sku_info = $advance_goods_info['sku_info']; if (!empty($sku_info['sku_id_list'])) { $map = array( 'id' => array('in', $sku_info['sku_id_list']) ); $name = $SkuListModel->getFindField($map, 'name', true); $sku_info['name'] = implode(',', $name); } else { $sku_info['name'] = ''; } $redis_key = 'retail_advance_goods_' . $advance_info['id'] . '_' . $advance_info['goods_id'] . '_' . $sku_info['sku_id'] . '_*'; $sku = json_encode($sku_info); $surplus_num = $sku_info['surplus_num']; //剩余库存 $purchase_price = $sku_info['purchase_price']; //成本价 $online_price = $sku_info['online_price']; //原价 $goods_img_id = $sku_info['goods_img_id']; //图片id $goods_name = $advance_info['goods_name'] . $sku_info['name']; break; case 1: $redis_key = 'retail_advance_goods_' . $advance_info['id'] . '_' . $advance_info['goods_id'] . '_0_*'; $surplus_num = $advance_info['surplus_num']; //剩余库存 $purchase_price = $advance_info['purchase_price']; //成本价 $online_price = $advance_info['online_price']; //原价 $goods_img_id = $advance_info['goods_img_id']; //图片id $goods_name = $advance_info['goods_name']; break; } // 获取待支付库存 $wait_pay_num = $this->getWaitNum($redis_key); //判断redis存入库存是否已经超时当前库存 if (round($surplus_num - $wait_pay_num - $goods['num'], 3) < 0) { $res = array( 'status' => 0, 'msg' => '商品' . $advance_info['goods_name'] . '库存不足.', ); $this->ajaxReturn($res); } $arrive_price_coupon = $advance_goods_info['arrive_price_coupon']; //单个抵现优惠金额 $earnest_price = $advance_goods_info['earnest_price']; //单个定金金额 $coupon_less_money = 0; //优惠券优惠金额 $all_earnest_price = 0; //总定金金额 $all_arrive_price = 0; //总抵现优惠金额 $vid = 0; $vipInfo = $this->getVipInfo(); if (!empty($vipInfo)) { $vid = $vipInfo['vid']; } $before_unit_price = $advance_goods_info['advance_price']; $all_advance_price = round($before_unit_price * $goods['num'], 2); $all_money = round($before_unit_price * $goods['num'], 2); if ($advance_info['advance_type'] == 2) { $all_earnest_price = round($earnest_price * $goods['num'], 2); if (!empty($arrive_price_coupon) && $arrive_price_coupon > 0) { $all_arrive_price = round($arrive_price_coupon * $goods['num'], 2); } $all_money = round($all_money - $all_arrive_price, 2); } $sell_info[] = array( 'num' => $goods['num'], //购买数量 'goods_num' => $surplus_num, 'goods_id' => $goods['goods_id'], 'price' => $advance_goods_info['advance_price'], 'all_money' => $all_money, 'type' => 1, 'store_id' => $store_id, 'merchant_id' => $merchant_id, 'goods_class_id' => $advance_info['goods_class_id'], 'goods_name' => $advance_info['goods_name'], 'pricing_method' => $advance_info['pricing_method'], 'purchase_price' => $purchase_price, 'original_price' => $online_price, 'goods_img_id' => $goods_img_id, 'attribute_state' => $advance_info['attribute_state'], 'sell_type' => 1, 'goods_type' => 1, 'vid' => $vid ? $vid : 0, 'uid' => $uid, 'sku_json_info' => $sku, 'sell_number' => $goods['num'], 'pa_state' => 0, 'coupon_money' => 0, 'use_integral' => 0, 'get_integral' => 0, 'integral_dis_money' => 0, 'goods_name_detail' => $goods_name, 'redis_key' => $redis_key, 'company' => $advance_info['company'] ); $all_money = $all_money ? $all_money : 0.01; if ($advance_info['advance_type'] == 1) { //判断是否满足优惠金额 $tail_money = $all_money; } else if ($advance_info['advance_type'] == 2) { $tail_money = round($all_money - $all_earnest_price, 2); } $ApiRedis = new \Addons\Api\RedisController(); //判断优惠券 if (!empty($coupon_type) && !empty($coupon_id)) { $CouponModel = D('RetailCoupon');//门店优惠券 if ($coupon_type == 1) { //门店的优惠券 $CouponLogsModel = D('RetailCouponLogs');//门店优惠券领取记录 $sMap = array( 'l.' . $CouponLogsModel::ID => $coupon_id, 'l.' . $CouponLogsModel::STORE_ID => $store_id, 'l.' . $CouponLogsModel::UID => $uid, 'l.' . $CouponLogsModel::STATUS => 1, //未使用的优惠券 'c.' . $CouponModel::BEGIN_TIME => array('lt', $time), //开始时间小于当前时间 'c.' . $CouponModel::END_TIME => array('gt', $time), //结束时间大于当前时间 'c.' . $CouponModel::NEED_MONEY => array('elt', $tail_money), 'c.' . $CouponModel::STORE_ID => $store_id ); $sFields = array( 'l.' . $CouponLogsModel::ID, 'c.' . $CouponModel::NEED_MONEY, 'c.' . $CouponModel::REDUCTION_MONEY, ); $coupon = $CouponLogsModel->getCoupon($sMap, $sFields); if (!empty($coupon)) { $coupon = $coupon[0]; if (round($coupon[$CouponModel::NEED_MONEY] - $tail_money, 2) > 0) { $res = array('status' => 0, 'msg' => '订单总额不足以使用该优惠券', 'data' => $sell_info); $this->ajaxReturn($res); } $coupon_less_money = $coupon[$CouponModel::REDUCTION_MONEY]; } else { $res = array('status' => 0, 'msg' => '优惠券异常', 'data' => $sell_info); $this->ajaxReturn($res); } } else { //商户优惠券 $VipMCouponModel = D('RetailVipMCoupon'); $map = array( 'v.' . $VipMCouponModel::IS_USE => 1, 'v.' . $VipMCouponModel::VID => $vid, 'v.' . $VipMCouponModel::START_TIME => array("LT", $time), 'v.' . $VipMCouponModel::END_TIME => array("GT", $time), 'v.' . $VipMCouponModel::MERCHANT_ID => $merchant_id, 'v.' . $VipMCouponModel::ID => $coupon_id, ); $fields = array( 'v.' . $VipMCouponModel::ID, 'v.' . $VipMCouponModel::NEED_MONEY, 'v.' . $VipMCouponModel::REDUCE_MONEY, 'v.' . $VipMCouponModel::ID, ); $couponRes = $VipMCouponModel->getFindUseCoupon($map, self::$store_id, $fields); if ($couponRes['status'] == 1) { $coupon = $couponRes['coupon']; } else { $this->ajaxReturn($couponRes); } if (round($coupon[$VipMCouponModel::NEED_MONEY] - $all_money, 2) > 0) { $res = array('status' => 0, 'msg' => '该订单不足以使用该优惠券', 'data' => $sell_info); $this->ajaxReturn($res); } $coupon_less_money = $coupon[$VipMCouponModel::REDUCE_MONEY]; } if (!empty($coupon_less_money)) { $sell_info = reduceOrderMoney($sell_info, floatval($coupon_less_money), $all_money, $SellModel::COUPON_MONEY); $sell_info = ascSortByFields($sell_info, $SellModel::PRICE); $all_money = round($all_money - $coupon_less_money, 2); $all_money = $all_money >= 0.01 ? $all_money : 0.01; $coupon_time = 602; $coupon_redis_key = 'retail_coupon_store_id_' . $store_id . '_uid_' . $uid . '_coupon_type_' . $coupon_type . '_coupon_id_' . $coupon_id; $parameter = array( 'coupon_id' => $coupon_id, 'coupon_type' => $coupon_type, 'uid' => $uid, 'coupon_redis_key' => $coupon_redis_key, 'coupon_time' => $coupon_time ); $CouponModel->frozen($parameter); } } else { $coupon_id = 0; $coupon_type = 0; } $user_address = ''; //判断商品发货方式设置 switch ($pickup_type) { case 2: //同城配送 $AddressModel = D('RetailAddress'); $response = $this->getLngLatAndFee($address_id); if ($response['status'] == 0) { $res = array('status' => 0, 'msg' => $response['msg']); $this->ajaxReturn($res); } if ($delivery_fee != $response['fee']) { $res = array('status' => 0, 'msg' => '配送费有变动,请退出重试'); $this->ajaxReturn($res); } $addressInfo = $response['address']; $link_name = $addressInfo[$AddressModel::NAME]; $mobile_phone = $addressInfo[$AddressModel::PHONE]; $user_address = $addressInfo[$AddressModel::ADDRESS]; break; case 3: //计算配送费 $CostCalculation = new CostCalculationController(); $AddressModel = D('RetailAddress'); $addressInfo = $AddressModel->getUserAddress($address_id); $link_name = $addressInfo['name']; $mobile_phone = $addressInfo['phone']; $user_address = $addressInfo['address']; $province = $addressInfo['province']; $city = $addressInfo['city']; $area = $addressInfo['area']; $cost_fee = $CostCalculation->getFreight(json_encode(array($goods)), $store_id, $province, $city, $area); if ($cost_fee['status'] == 1) { if ($cost_fee['delivery_fee'] != $delivery_fee) { $res = array('status' => 0, 'msg' => '快递费有变动,请退出重试'); $this->ajaxReturn($res); } } else { $this->ajaxReturn($cost_fee); } break; } //配送费 if ($delivery_fee && ($pickup_type == 2 || $pickup_type == 3)) { $all_money += $delivery_fee; } else { $delivery_fee = 0; } $all_money = round($all_money, 2); if ($all_money != $frontAllMoney) { $res['d'] = $all_money . ' ' . $frontAllMoney; $res['status'] = 0; $res['msg'] = '商品信息有变动,请退出重试'; $this->ajaxReturn($res); } // 判断是否为会员 if (!empty($vipInfo)) { $vip_type = $vipInfo['user_type']; $VipConfigModel = D('RetailVipConfig'); //获取商户会员设置 $vip_config = $VipConfigModel->getFind(array("merchantid" => self::$merchant_id)); if ($vip_config) { $give_state = true; //判断是否执行基于订单 if ($advance_info['integral_mem_status'] == 1 && $advance_info['integral'] > 0) { $parameter = array( 'vip_type' => $vip_type, 'vip_config' => $vip_config, 'integral' => $advance_info['integral'], 'num' => $num ); $integral = $VipConfigModel->buy_goods_get_integral($parameter); if ($integral > 0) { $give_state = false; } } if ($give_state) { $parameter = array( 'vip_type' => $vip_type, 'vip_config' => $vip_config ); $percent_state = $VipConfigModel->buy_integral_config($parameter); if ($percent_state) { $parameter['all_money'] = $all_money - $delivery_fee; $integral = $VipConfigModel->buy_get_integral($parameter); } } } } //添加bill_order表数据 $billData = array( 'vid' => $vid, 'pay_state' => 0, 'pay_type' => 0,//支付类型 'all_money' => $all_money, 'amount_received' => $all_money,//实收金额 'store_id' => $store_id, 'merchant_id' => $merchant_id, 'uid' => $uid, 'type' => 1,//出售类型 1:线上商城 2:店内 'add_time' => $time, ); $bill = $BillModel->addBillOrder($billData); if (!$bill) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } if (!empty($bill)) { $BillOnlineModel = D('RetailBillOnline'); $billOnlineData = array( 'bill_id' => $bill['id'], 'pickup_type' => $pickup_type, 'user_name' => $link_name, 'user_phone' => $mobile_phone, 'delivery_fee' => $delivery_fee, 'order_genre' => 4, 'remark' => $remark, 'user_select_time' => $user_select_time, ); if ($pickup_type == 2 || $pickup_type == 3) { $billOnlineData['user_address'] = $user_address; } $addOnline = $BillOnlineModel->addOrder($billOnlineData);//添加线上表数据 if ($addOnline) { $BillDisModel = D('RetailBillDiscount'); $billDisData = array( 'bill_id' => $bill['id'], 'get_integral' => ($vipInfo && $integral) ? $integral : 0, ); if (!empty($coupon_id)) { $billDisData['coupon_money'] = $coupon_less_money; $billDisData['coupon_type'] = $coupon_type; $billDisData['coupon_id'] = $coupon_id; } $addDis = $BillDisModel->addOrder($billDisData);//添加优惠表数据 if (empty($addDis)) { $res = array('status' => 0, 'msg' => '数据库错误,请重试'); $this->ajaxReturn($res); } $advance_order = array( 'bill_id' => $bill['id'], 'advance_id' => $advance_info['id'], 'goods_id' => $goods['goods_id'], 'all_advance_price' => $all_advance_price, 'buy_number' => $goods['num'], 'all_earnest_price' => $all_earnest_price, 'all_arrive_price' => $all_arrive_price, 'advance_type' => $advance_info['advance_type'], 'uid' => $uid, 'tail_end_time' => $advance_info['tail_end_time'], 'refuse_earnest_price' => $advance_info['refuse_earnest_price'] ); if ($advance_info['attribute_state'] == 2) { $advance_order['sku_id'] = $sku_info['sku_id']; } $addAdvance = D("RetailBillAdvance")->addOrder($advance_order); if (!$addAdvance) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } } else { $res = array('status' => 0, 'msg' => '数据库错误,请重试'); $this->ajaxReturn($res); } } $SmallProgramModel = D('RetailSmallProgramUser'); if ($pickup_type == 1) { $updateUser = array( 'link_name' => $link_name, 'mobile_phone' => $mobile_phone, ); $SmallProgramModel->updateOrder(array('id' => $uid), $updateUser);//更新小程序需用户信息 } if ($bill) { // 循环 添加 sell 表数据 $sell_redis = array(); foreach ($sell_info as $key => $val) { if (!empty($vipInfo) && $integral > 0) { $val['get_integral'] = $integral; } $val['bill_id'] = $bill['id']; $val['add_time'] = $time; $val['pay_state'] = 0; $redis_key = $val['redis_key']; $goods_num = $val['goods_num']; $goods_name = $val['goods_name_detail']; unset($val['redis_key']); unset($val['goods_num']); unset($val['goods_name_detail']); unset($val['guide_royalty_state']); unset($val['guide_royalty_custom_state']); unset($val['guide_royalty_online_price']); $add_sell = $SellModel->addOrder($val); if (!$add_sell) { $res = array('status' => 0, 'msg' => '抱歉,下单失败,请稍后再试'); $this->ajaxReturn($res); } $wait_pay_num = $this->getWaitNum($redis_key); // 判断库存 if (round($goods_num - $wait_pay_num - $val['num'], 3) < 0) { $res = array( 'goods_num' => $goods_num, 'wait_pay_num' => $wait_pay_num, 'num' => $val['num'], 'status' => 0, 'msg' => '商品' . $goods_name . '库存不足..', ); $this->ajaxReturn($res); } $redis_key_set = substr($redis_key, 0, -1); $redis_key_set = $redis_key_set . $uid . '_' . $bill['id']; // 下单成功,添加redis数据库记录 // 添加 设置数据类型 $redis_save = array( 'bill_id' => $bill['id'], 'num' => $val['num'], 'expiration_time' => time() + 602, 'key' => $redis_key_set, ); $set_result = $ApiRedis::$redis->setex($redis_key_set, 602, json_encode($redis_save)); if (!$set_result) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } $sell_redis[] = $redis_key_set; // 再次判断库存,获取待支付库存 $wait_pay_num = $this->getWaitNum($redis_key); // 判断规格,获取现有现有库存 if (round($goods_num - $wait_pay_num, 3) < -1) { $ApiRedis::$redis->delete($ApiRedis::$redis->keys('*_' . $bill['id'])); $res = array( 'goods_num' => $goods_num, 'wait_pay_num' => $wait_pay_num, 'num' => $val['num'], 'status' => 0, 'msg' => '商品' . $goods_name . '库存不足...', ); $this->ajaxReturn($res); } } $res['sell_redis'] = $sell_redis; $res['status'] = 1; $res['msg'] = '下单成功'; if ($advance_info['advance_type'] == 2) { $all_money = $all_earnest_price; //支付定金 } $res['data'] = array( 'order_no' => $bill['order_no'], 'allMoney' => $all_money, ); } else { $res = array('status' => 0, 'msg' => '下单失败'); } $this->ajaxReturn($res); } /** * 预售下单接口(定金,全额) * [pickup_type=>[1自提 2配送 3快递],all_money=>总价,address_id=>收货地址id, * coupon_id=>优惠券id,coupon_type=>优惠券类型,link_name=>姓名,mobile_phone=>电话, * delivery_fee=>配送费,remark=>用户留言,start_date=>配送或者自提时间, * advance_id=>预售活动,goods_id=>商品id,sku_id=>规格id,num=>购买数量] */ public function advance_place_order_v1() { if (empty(self::$uid) || self::$user['login_state'] != 1) { $res['status'] = -1; $res['msg'] = '用户未登录'; $this->ajaxReturn($res); } //验证参数列表及规则 $verifyItem = array( 'pickup_type' => array('in' => array(1, 2, 3), 'require_link' => array( array('require_data' => 1, 'link_key' => array('link_name', 'mobile_phone'))), array('require_data' => 2, 'link_key' => array('address_id')), array('require_data' => 3, 'link_key' => array('address_id')), ), 'all_money' => array('type' => 'money'), 'goods_id' => array('type' => 'number'), 'sku_id' => array('type' => 'number', 'require' => false), 'num' => array('type' => 'number'), 'address_id' => array('type' => 'number', 'min' => 1, 'require' => false), 'coupon_id' => array('type' => 'number', 'min' => 1, 'require' => false), 'coupon_type' => array('in' => array(1, 2, 3), 'require' => false), 'link_name' => array('type' => 'name', 'require' => false), 'mobile_phone' => array('type' => 'mobile', 'require' => false), 'delivery_fee' => array('type' => 'money', 'min' => 0, 'require' => false), 'remark' => array('type' => 'string', 'maxLen' => 255, 'require' => false), 'start_date' => array('type' => 'string', 'require' => false), ); $post = I('post.'); $verify = verifyRequestParam($post, $verifyItem);//验证参数是否正确 if ($verify['error'] == 1) { $result['status'] = 0; $result['msg'] = $verify['key'] . $verify['message']; $this->ajaxReturn($result); } $platform_type = self::$merchant['platform_type']; //商户平台类型 1默认系统 2微礼平台 $pickup_type = $post['pickup_type']; $frontAllMoney = $post['all_money']; $address_id = $post['address_id']; $coupon_id = $post['coupon_id']; $coupon_type = $post['coupon_type']; $link_name = $post['link_name']; $mobile_phone = $post['mobile_phone']; $delivery_fee = $post['delivery_fee']; $remark = $post['remark']; $user_select_time = $post['start_date']; $user_select_time = strtotime($user_select_time); $advance_id = $post['advance_id']; $goods_id = $post['goods_id']; $sku_id = $post['sku_id']; $num = $post['num']; $uid = self::$uid; $store_id = self::$store_id; $merchant_id = self::$merchant_id; $time = time(); $ver = $this->verifyPlaceOrder(); if (!$ver) { $this->ajaxReturn(array('status' => 0, 'msg' => '当前时间未在商家营业时间内')); } $AdvanceGoods = new AdvanceGoodsModel(); $advance_sale_status = $AdvanceGoods->advance_master_verify($store_id); //预售活动总开关验证 if ($advance_sale_status['status'] == 0) { $this->ajaxReturn($advance_sale_status); } $storeSet = $this->getStoreSet_v2();//获取门店设置 $delivery_set = $storeSet['delivery_set'];//存储是否开启自提、同城配送、快递发货 if ($delivery_set[$pickup_type - 1] != 1) { $res = array('status' => 0, 'msg' => '该配送方式已关闭'); $this->ajaxReturn($res); } $advance_info = $AdvanceGoods->advance_activity_state(array("merchant_id" => $merchant_id, "store_id" => $store_id, "advance_id" => $advance_id, "pickup_type" => $pickup_type)); //验证预售活动 if ($advance_info['status'] == 0) { $this->ajaxReturn($advance_info); } $advance_info = $advance_info['advance']; $goods = array( 'goods_id' => $goods_id, 'sku_id' => $sku_id, 'num' => $num, 'delivery_template_id' => $advance_info['delivery_template_id'], 'weight_value' => $advance_info['weight_value'], ); $advance_goods_info = $AdvanceGoods->advance_goods_state(array("goods" => $goods, "advance_info" => $advance_info, 'uid' => $uid, 'merchant_id' => $merchant_id, 'platform_type' => $platform_type, 'third_code' => self::$store['third_code'])); //验证预售商品 if ($advance_goods_info['status'] == 0) { $this->ajaxReturn($advance_goods_info); } $BillModel = D('RetailBillOrder');//订单主表 $SkuListModel = D('RetailSkuList'); $SellModel = D('RetailSell'); switch ($advance_info['attribute_state']) { case 2: $sku_info = $advance_goods_info['sku_info']; if ($platform_type == 2) { //第三方平台 $third_goods_code = $sku_info['third_goods_code']; } if (!empty($sku_info['sku_id_list'])) { $map = array( 'id' => array('in', $sku_info['sku_id_list']) ); $name = $SkuListModel->getFindField($map, 'name', true); $sku_info['name'] = implode(',', $name); } else { $sku_info['name'] = ''; } $redis_key = 'retail_advance_goods_' . $advance_info['id'] . '_' . $advance_info['goods_id'] . '_' . $sku_info['sku_id'] . '_*'; $sku = json_encode($sku_info); $surplus_num = $sku_info['surplus_num']; //剩余库存 $purchase_price = $sku_info['purchase_price']; //成本价 $online_price = $sku_info['online_price']; //原价 $goods_img_id = $sku_info['goods_img_id']; //图片id $goods_name = $advance_info['goods_name'] . $sku_info['name']; $goods['weight_value'] = $sku_info['weight_value']; break; case 1: if ($platform_type == 2) { //第三方平台 $third_goods_code = $advance_info['third_goods_code']; } $redis_key = 'retail_advance_goods_' . $advance_info['id'] . '_' . $advance_info['goods_id'] . '_0_*'; $surplus_num = $advance_info['surplus_num']; //剩余库存 $purchase_price = $advance_info['purchase_price']; //成本价 $online_price = $advance_info['online_price']; //原价 $goods_img_id = $advance_info['goods_img_id']; //图片id $goods_name = $advance_info['goods_name']; break; } // 获取待支付库存 $wait_pay_num = $this->getWaitNum($redis_key); //判断redis存入库存是否已经超时当前库存 if (round($surplus_num - $wait_pay_num - $goods['num'], 3) < 0) { $res = array( 'status' => 0, 'msg' => '商品' . $advance_info['goods_name'] . '库存不足.', ); $this->ajaxReturn($res); } $arrive_price_coupon = $advance_goods_info['arrive_price_coupon']; //单个抵现优惠金额 $earnest_price = $advance_goods_info['earnest_price']; //单个定金金额 $coupon_less_money = 0; //优惠券优惠金额 $all_earnest_price = 0; //总定金金额 $all_arrive_price = 0; //总抵现优惠金额 $vid = 0; $vipInfo = $this->getVipInfo(); if($platform_type == 2 && empty($vipInfo)){ $res = array( 'status' => 0, 'msg' => '请绑定会员卡', ); $this->ajaxReturn($res); } if (!empty($vipInfo)) { $vid = $vipInfo['vid']; } $before_unit_price = $advance_goods_info['advance_price'];//优惠前的单价 $all_advance_price = round($before_unit_price * $goods['num'], 2); //总预售价 $all_money = round($before_unit_price * $goods['num'], 2);//总价 if ($advance_info['advance_type'] == 2) { $all_earnest_price = round($earnest_price * $goods['num'], 2); if (!empty($arrive_price_coupon) && $arrive_price_coupon > 0) { $all_arrive_price = round($arrive_price_coupon * $goods['num'], 2); } $all_money = round($all_money - $all_arrive_price, 2); } $sell_info[] = array( 'num' => $goods['num'], //购买数量 'goods_num' => $surplus_num, 'goods_id' => $goods['goods_id'], 'price' => $advance_goods_info['advance_price'], 'all_money' => $all_money, 'type' => 1, 'store_id' => $store_id, 'merchant_id' => $merchant_id, 'goods_class_id' => $advance_info['goods_class_id'], 'goods_name' => $advance_info['goods_name'], 'pricing_method' => $advance_info['pricing_method'], 'purchase_price' => $purchase_price, 'original_price' => $online_price, 'goods_img_id' => $goods_img_id, 'attribute_state' => $advance_info['attribute_state'], 'sell_type' => 1, 'goods_type' => 1, 'vid' => $vid ? $vid : 0, 'uid' => $uid, 'sku_json_info' => $sku ? $sku : null, 'sku_id' => $sku_info['sku_id'] ? $sku_info['sku_id'] : 0, 'sell_number' => $goods['num'], 'pa_state' => 0, 'coupon_money' => 0, 'use_integral' => 0, 'get_integral' => 0, 'integral_dis_money' => 0, 'goods_name_detail' => $goods_name, 'redis_key' => $redis_key, 'company' => $advance_info['company'], 'serial_number_state' => $advance_info['serial_number_state'] ? $advance_info['serial_number_state'] : 0 ); $all_money = $all_money ? $all_money : 0.01; if ($advance_info['advance_type'] == 1) { //判断是否满足优惠金额 $tail_money = $all_money; } else if ($advance_info['advance_type'] == 2) { $tail_money = round($all_money - $all_earnest_price, 2); } $ApiRedis = new \Addons\Api\RedisController(); //免配送费门槛金额 $delivery_cost = self::$store['delivery_cost']; $delivery_status = self::$store['delivery_status']; $before_less_money = $all_money; //判断优惠券 if (!empty($coupon_type) && !empty($coupon_id)) { $CouponModel = D('RetailCoupon');//门店优惠券 $coupon_redis_key = 'retail_coupon_store_id_' . $store_id . '_uid_' . $uid . '_coupon_type_' . $coupon_type . '_coupon_id_' . $coupon_id; if (C('IS_SET_REDIS')) { $user_shop_coupon = $ApiRedis::$redis->mget($ApiRedis::$redis->keys($redis_key)); if (!empty($user_shop_coupon)) { $res['status'] = 0; $res['msg'] = '该优惠券正在核销中,请勿重复使用'; $res['data'] = $sell_info; $this->ajaxReturn($res); } } switch ($coupon_type) { case 1: //门店的优惠券 $CouponLogsModel = D('RetailCouponLogs');//门店优惠券领取记录 $sMap = array( 'l.' . $CouponLogsModel::ID => $coupon_id, 'l.' . $CouponLogsModel::STORE_ID => $store_id, 'l.' . $CouponLogsModel::UID => $uid, 'l.' . $CouponLogsModel::STATUS => 1, //未使用的优惠券 'c.' . $CouponModel::BEGIN_TIME => array('lt', $time), //开始时间小于当前时间 'c.' . $CouponModel::END_TIME => array('gt', $time), //结束时间大于当前时间 'c.' . $CouponModel::NEED_MONEY => array('elt', $tail_money), 'c.' . $CouponModel::STORE_ID => $store_id ); $sFields = array( 'l.' . $CouponLogsModel::ID, 'c.' . $CouponModel::NEED_MONEY, 'c.' . $CouponModel::REDUCTION_MONEY, ); $coupon = $CouponLogsModel->getCoupon($sMap, $sFields); if (!empty($coupon)) { $coupon = $coupon[0]; if (round($coupon[$CouponModel::NEED_MONEY] - $tail_money, 2) > 0) { $res = array('status' => 0, 'msg' => '订单总额不足以使用该优惠券', 'data' => $sell_info); $this->ajaxReturn($res); } $coupon_less_money = $coupon[$CouponModel::REDUCTION_MONEY]; } else { $res = array('status' => 0, 'msg' => '优惠券异常', 'data' => $sell_info); $this->ajaxReturn($res); } break; case 2: //会员普通优惠券 $CouponClass = new CouponUse(); $result = $CouponClass->PlaceOrderCoupon([ 'merchant_id' => self::$merchant_id, 'store_id' => self::$store_id, 'vip_id' => $vid, 'coupon_id' => $coupon_id, 'order_money' => $tail_money, 'sellDataArray' => $sell_info, ]); if ($result['status'] != 1) { $result['msg'] = $result['message']; $this->ajaxReturn($result); } $coupon = $result['data']['coupon']; //优惠券信息 $goods_type = $coupon['appoint_type']; //1 全部商品 2指定分类 3指定商品 $coupon_less_money = $coupon['discount_amount']; //优惠金额 $activity_goods = $result['data']['activity_goods'];//参与优惠的商品信息 $activity_goods_money = $result['data']['activity_goods_money']; //参与优惠的商品总金额 break; case 3: //商家券 $ShopCouponClass = new ShopCoupon(); $result = $ShopCouponClass->xcxPlaceOrder([ 'coupon_id' => $coupon_id, 'allMoney' => $tail_money, 'store_id' => self::$store_id, 'sellDataArray' => $sell_info ]); if ($result['status'] != 1) { $result['msg'] = $result['message']; $this->ajaxReturn($result); } $activity_goods = $result['data']['goods']; //参与优惠的商品信息 $shop_coupon = $result['data']['shop_coupon']; //商家券信息 $activity_goods_money = $result['data']['goods_money']; //参与优惠的商品总金额 $goods_type = $shop_coupon['goods_type']; //1所有商品 2指定商品 $coupon_less_money = json_decode($shop_coupon['coupon_use_rule'], true)['fixed_normal_coupon']['discount_amount']; //优惠金额 break; } if (!empty($coupon_less_money)) { if (($coupon_type == 3 || $coupon_type == 2) && ($goods_type == 2 || $goods_type == 3) && (count($activity_goods) != count($sell_info))) { //优惠券指定商品或分类 $activity_goods = ascSortByFields($activity_goods, 'price'); $CouponClass = new CouponUse(); $sellDataArray_new = reduceOrderMoney($activity_goods, floatval($coupon_less_money), $activity_goods_money, $SellModel::COUPON_MONEY); $sell_info = $CouponClass->sellDataArrayReplace([ 'sellDataArray' => $sell_info, 'sellDataArray_new' => $sellDataArray_new ]); } else { $sell_info = reduceOrderMoney($sell_info, floatval($coupon_less_money), $all_money, $SellModel::COUPON_MONEY); } $sell_info = ascSortByFields($sell_info, $SellModel::PRICE); $all_money = round($all_money - $coupon_less_money, 2); $all_money = $all_money >= 0.01 ? $all_money : 0.01; if (C('IS_SET_REDIS')) { $coupon_time = 602; $parameter = array( 'coupon_id' => $coupon_id, 'coupon_type' => $coupon_type, 'uid' => $uid, 'coupon_redis_key' => $coupon_redis_key, 'coupon_time' => $coupon_time ); $CouponModel->frozen($parameter); } } } else { $coupon_id = 0; $coupon_type = 0; } $user_address = ''; //判断商品发货方式设置 switch ($pickup_type) { case 2: //同城配送 $AddressModel = D('RetailAddress'); $response = $this->getLngLatAndFee($address_id); if ($response['status'] == 0) { $res = array('status' => 0, 'msg' => $response['msg']); $this->ajaxReturn($res); } if ($delivery_fee != $response['fee']) { $res = array('status' => 0, 'msg' => '配送费有变动,请退出重试'); $this->ajaxReturn($res); } $addressInfo = $response['address']; $link_name = $addressInfo[$AddressModel::NAME]; $mobile_phone = $addressInfo[$AddressModel::PHONE]; $user_address = $addressInfo[$AddressModel::ADDRESS]; break; case 3: //计算配送费 $CostCalculation = new CostCalculationController(); $AddressModel = D('RetailAddress'); $addressInfo = $AddressModel->getUserAddress($address_id); $link_name = $addressInfo['name']; $mobile_phone = $addressInfo['phone']; $user_address = $addressInfo['address']; $province = $addressInfo['province']; $city = $addressInfo['city']; $area = $addressInfo['area']; $cost_fee = $CostCalculation->getFreight(json_encode(array($goods)), $store_id, $province, $city, $area); if ($cost_fee['status'] == 1) { if ($cost_fee['delivery_fee'] != $delivery_fee) { $res = array('status' => 0, 'msg' => '快递费有变动,请退出重试'); $this->ajaxReturn($res); } } else { $this->ajaxReturn($cost_fee); } break; } if ($pickup_type != 1 && $delivery_status == 1) { if ($before_less_money >= $delivery_cost) { $delivery_fee = 0; } } //配送费 if ($delivery_fee && ($pickup_type == 2 || $pickup_type == 3)) { $all_money += $delivery_fee; } else { $delivery_fee = 0; } $all_money = round($all_money, 2); if ($all_money != $frontAllMoney) { $res['d'] = $all_money . ' ' . $frontAllMoney; $res['status'] = 0; $res['msg'] = '商品信息有变动,请退出重试'; $this->ajaxReturn($res); } // 判断是否为会员 if (!empty($vipInfo)) { $vip_type = $vipInfo['user_type']; $VipConfigModel = D('RetailVipConfig'); //获取商户会员设置 $vip_config = $VipConfigModel->getFind(array("merchantid" => self::$merchant_id)); if ($vip_config) { $give_state = true; //判断是否执行基于订单 if ($advance_info['integral_mem_status'] == 1 && $advance_info['integral'] > 0) { $parameter = array( 'vip_type' => $vip_type, 'vip_config' => $vip_config, 'integral' => $advance_info['integral'], 'num' => $num ); $integral = $VipConfigModel->buy_goods_get_integral($parameter); if ($integral > 0) { $give_state = false; } } if ($give_state) { $parameter = array( 'vip_type' => $vip_type, 'vip_config' => $vip_config ); $percent_state = $VipConfigModel->buy_integral_config($parameter); if ($percent_state) { $parameter['all_money'] = $all_money - $delivery_fee; $integral = $VipConfigModel->buy_get_integral($parameter); } } } } //添加bill_order表数据 $billData = array( 'vid' => $vid, 'pay_state' => 0, 'pay_type' => 0,//支付类型 'all_money' => $all_money, 'amount_received' => $all_money,//实收金额 'store_id' => $store_id, 'merchant_id' => $merchant_id, 'uid' => $uid, 'type' => 1,//出售类型 1:线上商城 2:店内 'add_time' => $time, ); $bill = $BillModel->addBillOrder($billData); if (!$bill) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } if (!empty($bill)) { if ($platform_type == 2) { //第三方平台 $third_order = [ 'Customer_Code' => self::$store['third_code'], 'Member_Code' => $vipInfo['card_num'], 'AreaName' => $user_address ? $user_address : '', 'Remark' => '', 'Swno' => $bill['order_no'], 'Details' => [ [ 'third_goods_code' => $third_goods_code, 'num' => $goods['num'], 'all_money' => $all_advance_price, 'company'=>$advance_info['company'] ] ] ]; $ThirdOrderClass = new Order(); $thirdResult = $ThirdOrderClass->orderInsert([ 'merchant_id' => $merchant_id, 'order_type' => 1, 'third_order' => json_encode($third_order) ]); if ($thirdResult['status'] == 0) { $res = array('status' => 0, 'msg' => 'Q3 ERP平台下单失败' . $thirdResult['message']); $this->ajaxReturn($res); } if ($thirdResult['status'] == 1 && !empty($thirdResult['data'])) { //修改订单 $BillModel->updateOrder(['id' => $bill['id']], [ 'platform_type' => 2, 'third_order' => $thirdResult['data'] ]); } } $BillOnlineModel = D('RetailBillOnline'); $billOnlineData = array( 'bill_id' => $bill['id'], 'pickup_type' => $pickup_type, 'user_name' => $link_name, 'user_phone' => $mobile_phone, 'delivery_fee' => $delivery_fee, 'order_genre' => 4, 'remark' => $remark, 'user_select_time' => $user_select_time, ); if ($pickup_type == 2 || $pickup_type == 3) { $billOnlineData['user_address'] = $user_address; } $addOnline = $BillOnlineModel->addOrder($billOnlineData);//添加线上表数据 if ($addOnline) { $BillDisModel = D('RetailBillDiscount'); $billDisData = array( 'bill_id' => $bill['id'], 'get_integral' => ($vipInfo && $integral) ? $integral : 0, ); if (!empty($coupon_id)) { $billDisData['coupon_money'] = $coupon_less_money; $billDisData['coupon_type'] = $coupon_type; $billDisData['coupon_id'] = $coupon_id; } $addDis = $BillDisModel->addOrder($billDisData);//添加优惠表数据 if (empty($addDis)) { $res = array('status' => 0, 'msg' => '数据库错误,请重试'); $this->ajaxReturn($res); } $advance_order = array( 'bill_id' => $bill['id'], 'advance_id' => $advance_info['id'], 'goods_id' => $goods['goods_id'], 'all_advance_price' => $all_advance_price, 'buy_number' => $goods['num'], 'all_earnest_price' => $all_earnest_price, 'all_arrive_price' => $all_arrive_price, 'advance_type' => $advance_info['advance_type'], 'uid' => $uid, 'tail_end_time' => $advance_info['tail_end_time'], 'refuse_earnest_price' => $advance_info['refuse_earnest_price'] ); if ($advance_info['attribute_state'] == 2) { $advance_order['sku_id'] = $sku_info['sku_id']; } $addAdvance = D("RetailBillAdvance")->addOrder($advance_order); if (!$addAdvance) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } } else { $res = array('status' => 0, 'msg' => '数据库错误,请重试'); $this->ajaxReturn($res); } } $SmallProgramModel = D('RetailSmallProgramUser'); if ($pickup_type == 1) { $updateUser = array( 'link_name' => $link_name, 'mobile_phone' => $mobile_phone, ); $SmallProgramModel->updateOrder(array('id' => $uid), $updateUser);//更新小程序需用户信息 } if ($bill) { // 循环 添加 sell 表数据 $sell_redis = array(); foreach ($sell_info as $key => $val) { if (!empty($vipInfo) && $integral > 0) { $val['get_integral'] = $integral; } $val['bill_id'] = $bill['id']; $val['add_time'] = $time; $val['pay_state'] = 0; $redis_key = $val['redis_key']; $goods_num = $val['goods_num']; $goods_name = $val['goods_name_detail']; unset($val['redis_key']); unset($val['goods_num']); unset($val['goods_name_detail']); unset($val['guide_royalty_state']); unset($val['guide_royalty_custom_state']); unset($val['guide_royalty_online_price']); $add_sell = $SellModel->addOrder($val); if (!$add_sell) { $res = array('status' => 0, 'msg' => '抱歉,下单失败,请稍后再试'); $this->ajaxReturn($res); } if($platform_type != 2){ $wait_pay_num = $this->getWaitNum($redis_key); // 判断库存 if (round($goods_num - $wait_pay_num - $val['num'], 3) < 0) { $res = array( 'goods_num' => $goods_num, 'wait_pay_num' => $wait_pay_num, 'num' => $val['num'], 'status' => 0, 'msg' => '商品' . $goods_name . '库存不足..', ); $this->ajaxReturn($res); } } $redis_key_set = substr($redis_key, 0, -1); $redis_key_set = $redis_key_set . $uid . '_' . $bill['id']; // 下单成功,添加redis数据库记录 // 添加 设置数据类型 $redis_save = array( 'bill_id' => $bill['id'], 'num' => $val['num'], 'expiration_time' => time() + 602, 'key' => $redis_key_set, ); $set_result = $ApiRedis::$redis->setex($redis_key_set, 602, json_encode($redis_save)); if (!$set_result) { $res = array('status' => 0, 'msg' => '下单失败'); $this->ajaxReturn($res); } $sell_redis[] = $redis_key_set; if($platform_type != 2){ // 再次判断库存,获取待支付库存 $wait_pay_num = $this->getWaitNum($redis_key); // 判断规格,获取现有现有库存 if (round($goods_num - $wait_pay_num, 3) < -1) { $ApiRedis::$redis->delete($ApiRedis::$redis->keys('*_' . $bill['id'])); $res = array( 'goods_num' => $goods_num, 'wait_pay_num' => $wait_pay_num, 'num' => $val['num'], 'status' => 0, 'msg' => '商品' . $goods_name . '库存不足...', ); $this->ajaxReturn($res); } } } $res['sell_redis'] = $sell_redis; $res['status'] = 1; $res['msg'] = '下单成功'; if ($advance_info['advance_type'] == 2) { $all_money = $all_earnest_price; //支付定金 } $res['data'] = array( 'order_no' => $bill['order_no'], 'allMoney' => $all_money, ); } else { $res = array('status' => 0, 'msg' => '下单失败'); } $this->ajaxReturn($res); } /** * 尾款支付信息 */ public function advance_tail_info() { $order_no = I("post.order_no"); if (empty($order_no)) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } $bill_advance = D("RetailBillAdvance")->tail_verify(array('order_no' => $order_no)); //验证查询订单信息 if ($bill_advance['status'] == 0) { $this->ajaxReturn($bill_advance); } $bill_info = $bill_advance['data']['bill_info']; $tail_money = round($bill_info['all_money'] - $bill_info['all_earnest_price'], 2); if (C('IS_SET_REDIS')) { $ApiRedis = new \Addons\Api\RedisController(); $bill_info['sku_id'] = $bill_info['sku_id'] ? $bill_info['sku_id'] : 0; $redis_key = 'retail_advance_goods_' . $bill_info['advance_id'] . '_' . $bill_info['goods_id'] . '_' . $bill_info['sku_id'] . '_' . $bill_info['uid'] . '_' . $bill_info['bill_id']; $redis_save = array( 'bill_id' => $bill_info['bill_id'], 'num' => 0, 'expiration_time' => time() + 602, 'key' => $redis_key, ); $set_result = $ApiRedis::$redis->setex($redis_key, 602, json_encode($redis_save)); if (!$set_result) { $res = array('status' => 0, 'msg' => '尾款失败'); $this->ajaxReturn($res); } } $res = array( 'status' => 1, 'msg' => '验证成功', 'data' => array( 'all_money' => $tail_money ) ); $this->ajaxReturn($res); } /** * 获取下单参数 */ public function get_pay_info() { $advance_id = I("post.id"); if (empty($advance_id)) { $res = array('status' => 0, 'msg' => '参数缺失'); $this->ajaxReturn($res); } $advance_info = D("AdvanceGoods")->getFind(array('id' => $advance_id)); if (empty($advance_info) || $advance_info['advance_state'] == 0) { $res = array('status' => 0, 'msg' => '该预售活动已下架'); $this->ajaxReturn($res); } $storeSet = $this->getStoreSet_v2();//基本设置 $delivery_set = $storeSet['delivery_set']; $advance_delivery_type = explode(',', $advance_info['delivery_type']); foreach ($delivery_set as $key => $val) { if ($delivery_set[$key] == 1 && !in_array($key + 1, $advance_delivery_type)) { $delivery_set[$key] = 0; } } $storeSet['delivery_set'] = $delivery_set; $SmallProgramModel = D('RetailSmallProgramUser'); $address = $this->getDefaultAddress_v1();//默认地址 $ConfigModel = D('RetailMerchantConfig'); $merchantConfig = $ConfigModel->getConfig(self::$merchant_id); $delivery_cost = self::$store['delivery_cost']; $delivery_status = self::$store['delivery_status']; $res = array( 'status' => 1, 'msg' => '获取成功', 'data' => array( 'link_name' => self::$user[$SmallProgramModel::LINK_NAME], 'mobile_phone' => self::$user[$SmallProgramModel::MOBILE_PHONE], 'storeSet' => $storeSet, 'address' => $address, 'merchantConfig' => $merchantConfig ), 'delivery_cost' => $delivery_cost, 'delivery_status' => $delivery_status, ); $this->ajaxReturn($res); } }