/** * 生成订单 */ function cart3() { $accept_name = IFilter::act(IReq::get('accept_name')); $province = IFilter::act(IReq::get('province'), 'int'); $city = IFilter::act(IReq::get('city'), 'int'); $area = IFilter::act(IReq::get('area'), 'int'); $address = IFilter::act(IReq::get('address')); $mobile = IFilter::act(IReq::get('mobile')); $telphone = IFilter::act(IReq::get('telphone')); $zip = IFilter::act(IReq::get('zip')); $delivery_id = IFilter::act(IReq::get('delivery_id'), 'int'); $accept_time = IFilter::act(IReq::get('accept_time')); $payment = IFilter::act(IReq::get('payment'), 'int'); $order_message = IFilter::act(IReq::get('message')); $ticket_id = IFilter::act(IReq::get('ticket_id'), 'int'); $taxes = IFilter::act(IReq::get('taxes'), 'float'); $insured = IFilter::act(IReq::get('insured'), 'float'); $tax_title = IFilter::act(IReq::get('tax_title'), 'text'); $gid = IFilter::act(IReq::get('direct_gid'), 'int'); $num = IFilter::act(IReq::get('direct_num'), 'int'); $type = IFilter::act(IReq::get('direct_type')); //商品或者货品 $promo = IFilter::act(IReq::get('direct_promo')); $active_id = IFilter::act(IReq::get('direct_active_id'), 'int'); $takeself = IFilter::act(IReq::get('takeself'), 'int'); $order_no = Order_Class::createOrderNum(); $order_type = 0; $dataArray = array(); //pr($_POST); //防止表单重复提交 if (IReq::get('timeKey') != null) { if (ISafe::get('timeKey') == IReq::get('timeKey')) { IError::show(403, '订单数据不能被重复提交'); exit; } else { ISafe::set('timeKey', IReq::get('timeKey')); } } if ($province == 0 || $city == 0 || $area == 0) { IError::show(403, '请填写收货地址的省市地区'); } if ($delivery_id == 0) { IError::show(403, '请选择配送方式'); } $user_id = $this->user['user_id'] == null ? 0 : $this->user['user_id']; //配送方式,判断是否为货到付款 $deliveryObj = new IModel('delivery'); $deliveryRow = $deliveryObj->getObj('id = ' . $delivery_id); if ($deliveryRow['type'] == 0) { if ($payment == 0) { IError::show(403, '请选择正确的支付方式'); } } else { if ($deliveryRow['type'] == 1) { $payment = 0; } else { if ($deliveryRow['type'] == 2) { if ($takeself == 0) { IError::show(403, '请选择正确的自提点'); } } } } //如果不是自提方式自动清空自提点 if ($deliveryRow['type'] != 2) { $takeself = 0; } //计算费用 $countSumObj = new CountSum($user_id); //直接购买商品方式 if ($type && $gid) { //计算$gid商品 $goodsResult = $countSumObj->direct_count($gid, $type, $num, $promo, $active_id); } else { //计算购物车中的商品价格$goodsResult $goodsResult = $countSumObj->cart_count(); //清空购物车 IInterceptor::reg("cart@onFinishAction"); } //判断商品商品是否存在 if (is_string($goodsResult) || empty($goodsResult['goodsList'])) { IError::show(403, '商品数据错误'); exit; } //需要选择的地址数 $need_choose_addr_num = 1; if (count($goodsResult['goodsList']) == 1 && $goodsResult['count'] > 1) { $need_choose_addr_num = $goodsResult['count']; } //匹配收获地址 $address_arr = IFilter::act(IReq::get('radio_address'), 'int'); if (count($address_arr) != $need_choose_addr_num) { IError::show(403, '请选择' . $need_choose_addr_num . '个收货地址'); exit; } //加入促销活动 if ($promo && $active_id) { $activeObject = new Active($promo, $active_id, $user_id, $gid, $type, $num); $order_type = $activeObject->getOrderType(); } //获取红包减免金额 if ($ticket_id != '') { $memberObj = new IModel('member'); $memberRow = $memberObj->getObj('user_id = ' . $user_id, 'prop,custom'); if (ISafe::get('ticket_' . $ticket_id) == $ticket_id || stripos(',' . trim($memberRow['prop'], ',') . ',', ',' . $ticket_id . ',') !== false) { $propObj = new IModel('prop'); $ticketRow = $propObj->getObj('id = ' . $ticket_id . ' and NOW() between start_time and end_time and type = 0 and is_close = 0 and is_userd = 0 and is_send = 1'); if (!empty($ticketRow)) { $dataArray['prop'] = $ticket_id; } //锁定红包状态 $propObj->setData(array('is_close' => 2)); $propObj->update('id = ' . $ticket_id); } } $paymentObj = new IModel('payment'); $paymentRow = $paymentObj->getObj('id = ' . $payment, 'type,name'); $paymentName = $paymentRow['name']; $paymentType = $paymentRow['type']; //记录seller_id $seller_id = 0; if ($gid) { $goodsObj = new IModel("goods"); $sell = $goodsObj->getObj("id = " . $gid, 'seller_id'); if ($sell['seller_id'] > 0) { $seller_id = $sell['seller_id']; } } //最终订单金额计算 $orderData = $countSumObj->countOrderFee($goodsResult['sum'], $goodsResult['final_sum'], $goodsResult['weight'], $province, $delivery_id, $payment, $goodsResult['freeFreight'], $insured, $taxes); //生成的订单数据 $dataArray = array('order_no' => $order_no, 'user_id' => $user_id, 'accept_name' => $accept_name, 'pay_type' => $payment, 'distribution' => $delivery_id, 'postcode' => $zip, 'telphone' => $telphone, 'province' => $province, 'city' => $city, 'area' => $area, 'address' => $address, 'mobile' => $mobile, 'create_time' => ITime::getDateTime(), 'postscript' => $order_message, 'accept_time' => $accept_time, 'exp' => $goodsResult['exp'], 'point' => $goodsResult['point'], 'type' => $order_type, 'prop' => isset($dataArray['prop']) ? $dataArray['prop'] : null, 'payable_amount' => $goodsResult['sum'], 'real_amount' => $goodsResult['final_sum'], 'payable_freight' => $orderData['deliveryOrigPrice'], 'real_freight' => $orderData['deliveryPrice'], 'pay_fee' => $orderData['paymentPrice'], 'invoice' => $taxes ? 1 : 0, 'invoice_title' => $tax_title, 'taxes' => $taxes, 'promotions' => $goodsResult['proReduce'] + $goodsResult['reduce'] + (isset($ticketRow['value']) ? $ticketRow['value'] : 0), 'order_amount' => $orderData['orderAmountPrice'] - (isset($ticketRow['value']) ? $ticketRow['value'] : 0), 'if_insured' => $insured ? 1 : 0, 'insured' => $insured, 'takeself' => $takeself, 'active_id' => $active_id, 'seller_id' => $seller_id, 'address_id' => implode(',', $address_arr)); $dataArray['order_amount'] = $dataArray['order_amount'] <= 0 ? 0 : $dataArray['order_amount']; if ($seller_id) { $sellerObj = new IModel('seller'); $seller = $sellerObj->getObj("id = '{$seller_id}'"); if ($seller) { //商家的预存款不足以扣除订单总价的1.2% $nedd = floatval($dataArray['order_amount'] * (1.2 * 0.01)); if ($seller['balance'] <= 0 || $seller['balance'] < $nedd) { IError::show(403, '商家预存款不足,不能进行购买'); exit; } } } $orderObj = new IModel('order'); $orderObj->setData($dataArray); $this->order_id = $orderObj->add(); if ($this->order_id == false) { IError::show(403, '订单生成错误'); } /*将订单中的商品插入到order_goods表*/ $orderInstance = new Order_Class(); $orderInstance->insertOrderGoods($this->order_id, $goodsResult); //记录用户默认习惯的数据 if (!isset($memberRow['custom'])) { $memberObj = new IModel('member'); $memberRow = $memberObj->getObj('user_id = ' . $user_id, 'custom'); } $memberData = array('custom' => serialize(array('payment' => $payment, 'delivery' => $delivery_id, 'takeself' => $takeself))); $memberObj->setData($memberData); $memberObj->update('user_id = ' . $user_id); //收货地址的处理 /* if($user_id) { $addressObj = new IModel('address'); //如果用户之前没有收货地址,那么会自动记录此次的地址信息并且为默认 $addressRow = $addressObj->getObj('user_id = '.$user_id); if(empty($addressRow)) { $addressData = array('default'=>'1','user_id'=>$user_id,'accept_name'=>$accept_name,'province'=>$province,'city'=>$city,'area'=>$area,'address'=>$address,'zip'=>$zip,'telphone'=>$telphone,'mobile'=>$mobile); $addressObj->setData($addressData); $addressObj->add(); } else { //如果用户有收货地址,但是没有设置默认项,那么会自动设置此次地址信息为默认 $radio_address = intval(IReq::get('radio_address')); if($radio_address != 0) { $addressDefRow = $addressObj->getObj('user_id = '.$user_id.' and `default` = 1'); if(empty($addressDefRow)) { $addressData = array('default' => 1); $addressObj->setData($addressData); $addressObj->update('user_id = '.$user_id.' and id = '.$radio_address); } } } } */ //获取备货时间 $siteConfigObj = new Config("site_config"); $site_config = $siteConfigObj->getInfo(); $this->stockup_time = isset($site_config['stockup_time']) ? $site_config['stockup_time'] : 2; //数据渲染 $this->order_num = $dataArray['order_no']; $this->final_sum = $dataArray['order_amount']; $this->payment = $paymentName; $this->paymentType = $paymentType; $this->delivery = $deliveryRow['name']; $this->tax_title = $tax_title; $this->deliveryType = $deliveryRow['type']; //订单金额为0时,订单自动完成 if ($this->final_sum <= 0) { $order_id = Order_Class::updateOrderStatus($dataArray['order_no']); if ($order_id) { if ($user_id) { $this->redirect('/site/success/message/' . urlencode("订单确认成功,等待发货") . '/?callback=ucenter/order_detail/id/' . $order_id); } else { $this->redirect('/site/success/message/' . urlencode("订单确认成功,等待发货")); } } else { IError::show(403, '订单修改失败'); } } else { $this->setRenderData($dataArray); $this->redirect('cart3'); } }
/** * 生成订单 */ function cart3() { $accept_name = IFilter::act(IReq::get('accept_name')); $province = IFilter::act(IReq::get('province'), 'int'); $city = IFilter::act(IReq::get('city'), 'int'); $area = IFilter::act(IReq::get('area'), 'int'); $address = IFilter::act(IReq::get('address')); $mobile = IFilter::act(IReq::get('mobile')); $telphone = IFilter::act(IReq::get('telphone')); $zip = IFilter::act(IReq::get('zip')); $delivery_id = IFilter::act(IReq::get('delivery_id'), 'int'); $accept_time = IFilter::act(IReq::get('accept_time')); $payment = IFilter::act(IReq::get('payment'), 'int'); $order_message = IFilter::act(IReq::get('message')); $ticket_id = IFilter::act(IReq::get('ticket_id'), 'int'); $taxes = IFilter::act(IReq::get('taxes'), 'int'); $tax_title = IFilter::act(IReq::get('tax_title')); $gid = IFilter::act(IReq::get('direct_gid'), 'int'); $num = IFilter::act(IReq::get('direct_num'), 'int'); $type = IFilter::act(IReq::get('direct_type')); //商品或者货品 $promo = IFilter::act(IReq::get('direct_promo')); $active_id = IFilter::act(IReq::get('direct_active_id'), 'int'); $takeself = IFilter::act(IReq::get('takeself'), 'int'); $ticketUserd = IFilter::act(IReq::get('ticketUserd'), 'int'); $order_type = 0; $dataArray = array(); //防止表单重复提交 if (IReq::get('timeKey') != null) { if (ISafe::get('timeKey') == IReq::get('timeKey')) { IError::show(403, '订单数据不能被重复提交'); exit; } else { ISafe::set('timeKey', IReq::get('timeKey')); } } if ($province == 0 || $city == 0 || $area == 0) { IError::show(403, '请填写收货地址的省市地区'); } if ($delivery_id == 0) { IError::show(403, '请选择配送方式'); } $user_id = $this->user['user_id'] == null ? 0 : $this->user['user_id']; //配送方式,判断是否为货到付款 $deliveryObj = new IModel('delivery'); $deliveryRow = $deliveryObj->getObj('id = ' . $delivery_id); if ($deliveryRow['type'] == 0) { if ($payment == 0) { IError::show(403, '请选择正确的支付方式'); } } else { if ($deliveryRow['type'] == 1) { $payment = 0; } else { if ($deliveryRow['type'] == 2) { if ($takeself == 0) { IError::show(403, '请选择正确的自提点'); } } } } //如果不是自提方式自动清空自提点 if ($deliveryRow['type'] != 2) { $takeself = 0; } //计算费用 $countSumObj = new CountSum($user_id); $goodsResult = $countSumObj->cart_count($gid, $type, $num, $promo, $active_id); if (!$gid) { //清空购物车 IInterceptor::reg("cart@onFinishAction"); } //判断商品商品是否存在 if (is_string($goodsResult) || empty($goodsResult['goodsList'])) { IError::show(403, '商品数据错误'); exit; } //加入促销活动 if ($promo && $active_id) { $activeObject = new Active($promo, $active_id, $user_id, $gid, $type, $num); $order_type = $activeObject->getOrderType(); } $paymentObj = new IModel('payment'); $paymentRow = $paymentObj->getObj('id = ' . $payment, 'type,name'); $paymentName = $paymentRow['name']; $paymentType = $paymentRow['type']; //最终订单金额计算 $orderData = $countSumObj->countOrderFee($goodsResult, $province, $delivery_id, $payment, $taxes, 0, $promo, $active_id); if (is_string($orderData)) { IError::show(403, $orderData); exit; } //根据商品所属商家不同批量生成订单 $orderIdArray = array(); $orderNumArray = array(); $final_sum = 0; foreach ($orderData as $seller_id => $goodsResult) { //生成的订单数据 $dataArray = array('order_no' => Order_Class::createOrderNum(), 'user_id' => $user_id, 'accept_name' => $accept_name, 'pay_type' => $payment, 'distribution' => $delivery_id, 'postcode' => $zip, 'telphone' => $telphone, 'province' => $province, 'city' => $city, 'area' => $area, 'address' => $address, 'mobile' => $mobile, 'create_time' => ITime::getDateTime(), 'postscript' => $order_message, 'accept_time' => $accept_time, 'exp' => $goodsResult['exp'], 'point' => $goodsResult['point'], 'type' => $order_type, 'payable_amount' => $goodsResult['sum'], 'real_amount' => $goodsResult['final_sum'], 'payable_freight' => $goodsResult['deliveryOrigPrice'], 'real_freight' => $goodsResult['deliveryPrice'], 'pay_fee' => $goodsResult['paymentPrice'], 'invoice' => $taxes ? 1 : 0, 'invoice_title' => $tax_title, 'taxes' => $goodsResult['taxPrice'], 'promotions' => $goodsResult['proReduce'] + $goodsResult['reduce'], 'order_amount' => $goodsResult['orderAmountPrice'], 'insured' => $goodsResult['insuredPrice'], 'takeself' => $takeself, 'active_id' => $active_id, 'seller_id' => $seller_id, 'note' => ''); //获取红包减免金额 if ($ticket_id && $ticketUserd == $seller_id) { $memberObj = new IModel('member'); $memberRow = $memberObj->getObj('user_id = ' . $user_id, 'prop,custom'); //游客手动添加或注册用户道具中已有的代金券 if (ISafe::get('ticket_' . $ticket_id) == $ticket_id || stripos(',' . trim($memberRow['prop'], ',') . ',', ',' . $ticket_id . ',') !== false) { $propObj = new IModel('prop'); $ticketRow = $propObj->getObj('id = ' . $ticket_id . ' and NOW() between start_time and end_time and type = 0 and is_close = 0 and is_userd = 0 and is_send = 1'); if (!$ticketRow) { IError::show(403, '代金券不可用'); } if ($ticketRow['seller_id'] == 0 || $ticketRow['seller_id'] == $seller_id) { $ticketRow['value'] = $ticketRow['value'] >= $goodsResult['final_sum'] ? $goodsResult['final_sum'] : $ticketRow['value']; $dataArray['prop'] = $ticket_id; $dataArray['promotions'] += $ticketRow['value']; $dataArray['order_amount'] -= $ticketRow['value']; $goodsResult['promotion'][] = array("plan" => "代金券", "info" => "使用了¥" . $ticketRow['value'] . "代金券"); //锁定红包状态 $propObj->setData(array('is_close' => 2)); $propObj->update('id = ' . $ticket_id); } } } //促销规则 if (isset($goodsResult['promotion']) && $goodsResult['promotion']) { foreach ($goodsResult['promotion'] as $key => $val) { $dataArray['note'] .= " 【" . $val['info'] . "】 "; } } $dataArray['order_amount'] = $dataArray['order_amount'] <= 0 ? 0 : $dataArray['order_amount']; //生成订单插入order表中 $orderObj = new IModel('order'); $orderObj->setData($dataArray); $order_id = $orderObj->add(); if ($order_id == false) { IError::show(403, '订单生成错误'); } /*将订单中的商品插入到order_goods表*/ $orderInstance = new Order_Class(); $orderInstance->insertOrderGoods($order_id, $goodsResult['goodsResult']); //订单金额小于等于0直接免单 if ($dataArray['order_amount'] <= 0) { Order_Class::updateOrderStatus($dataArray['order_no']); } else { $orderIdArray[] = $order_id; $orderNumArray[] = $dataArray['order_no']; $final_sum += $dataArray['order_amount']; } } //记录用户默认习惯的数据 if (!isset($memberRow['custom'])) { $memberObj = new IModel('member'); $memberRow = $memberObj->getObj('user_id = ' . $user_id, 'custom'); } $memberData = array('custom' => serialize(array('payment' => $payment, 'delivery' => $delivery_id, 'takeself' => $takeself))); $memberObj->setData($memberData); $memberObj->update('user_id = ' . $user_id); //收货地址的处理 if ($user_id) { $addressObj = new IModel('address'); $addressDefRow = $addressObj->getObj('user_id = ' . $user_id . ' and `default` = 1'); if (!$addressDefRow) { $radio_address = IFilter::act(IReq::get('radio_address'), 'int'); $addressObj->setData(array('default' => 1)); $addressObj->update('user_id = ' . $user_id . ' and id = ' . $radio_address); } } //获取备货时间 $siteConfigObj = new Config("site_config"); $site_config = $siteConfigObj->getInfo(); $this->stockup_time = isset($site_config['stockup_time']) ? $site_config['stockup_time'] : 2; //数据渲染 $this->order_id = join(",", $orderIdArray); $this->final_sum = $final_sum; $this->order_num = join(",", $orderNumArray); $this->payment = $paymentName; $this->paymentType = $paymentType; $this->delivery = $deliveryRow['name']; $this->tax_title = $tax_title; $this->deliveryType = $deliveryRow['type']; //订单金额为0时,订单自动完成 if ($this->final_sum <= 0) { $this->redirect('/site/success/message/' . urlencode("订单确认成功,等待发货") . '/?callback=ucenter/order'); } else { $this->setRenderData($dataArray); $this->redirect('cart3'); } }