address = $this->getAddressData(); } public function save() { if (!$this->validate()) return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; if(empty($this->address)){ return [ 'code' => 1, 'msg' => '请指定收货地址', ]; } try { $form = new OrderSubmitPreviewForm(); $form->attributes = $this->attributes; $form->store_id = $this->store_id; $form->user_id = $this->user_id; $form->cart_id_list = $this->cart_id_list; $form->attr_list = $this->attr_list; $form->goods_id = $this->goods_id; $form->is_pay = 0; $prev = $form->search(); if($prev['code'] != 0){ return $prev; } $mchListData = $prev['data']; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } $order_id_list = []; /** * @var $address Address */ $address = (object)($this->address); $t = \Yii::$app->db->beginTransaction(); foreach ($mchListData['goods_list'] as $mch) { $mch_goods = $mch['goods_list']; $check_res = $this->checkAdmin($mch_goods); if ($check_res['code'] === 1) { return $check_res; } // if($mch['in_rule'] == 0){ // $t->rollBack(); // return [ // 'code' => 1, // 'msg' => '收货地址不支持快递', // ]; // } if ($mch_goods) { $data = [ 'province_id' => $address->province_id, 'city_id' => $address->city_id, 'district_id' => $address->district_id, ]; //代理检测 $address_res = $this->checkAddress($data, $mch_goods); if ($address_res['code'] !== 0) { return $address_res; } //胜天半子检测 $stbz_res = $this->checkStbz($mch_goods, $address); if ($stbz_res['code'] !== 0) { return $stbz_res; } $order = new PurchaseOrder(); $order->store_id = $this->store_id; $order->user_id = $this->user_id; $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_ALLIANCE_PURCHASE); $order->pay_price = $mchListData['express_price_total'] + $mchListData['price_total']; $order->supplier_id = $mch['supplier']['id']; $order->cloud_order_id = $mchListData['cloud_order_id']; $order->created_at = time(); $order->remark = $this->remark ? : ''; if ($this->payment == 2) { $order->pay_type = 2; $order->is_pay = 0; } if ($this->payment == 3) { $order->pay_type = 3; $order->is_pay = 0; } $order->address = $address->province . $address->city . $address->district . $address->detail; $order->mobile = $address->mobile; $order->name = $address->name; $order->province_id = $address->province_id; $order->city_id = $address->city_id; $order->district_id = $address->district_id; $order->address_data = json_encode([ 'province' => trim($address->province), 'city' => trim($address->city), 'district' => trim($address->district), 'detail' => trim($address->detail), 'latitude' => $address->latitude, 'longitude' => $address->longitude, ], JSON_UNESCAPED_UNICODE); $order->express_price = $mchListData['express_price_total']; $order->total_price = $mchListData['price_total']; $order->saas_id = get_saas_user_id(); if ($order->save()) { // 处理订单生成之后其他相关数据 $orderRes = $this->insertData($mch, $order); if ($orderRes['code'] == 1) { $t->rollBack(); return $orderRes; } // $printer_order = new PrintOrder($order->store_id, $order->id, 'order', 0, 0); // $printer_order->print_order(); // 订单提交完成发送消息 // NoticeSend::OrderSubmit($this->user_id, $order->mobile, $order->order_no, $order->pay_price, $mch_goods[0]['goods_name']); $order_id_list[] = $order->id; } else { $t->rollBack(); return $this->getErrorResponse($order); } } } if (count($order_id_list) > 0) { $t->commit(); //多个订单合并 return [ 'code' => 0, 'msg' => '订单提交成功', 'data' => (object)[ 'order_id_list' => $order_id_list, 'add_time' => $order->created_at ], ]; } else { $t->rollBack(); return [ 'code' => 1, 'msg' => '订单提交失败', 'order_list' => $order_id_list ]; } } public function checkAdmin($goods_info) { //判断是否有集采商品且存在区域代理 foreach ($goods_info as $item) { if ((int)$item['goods']['send_type'] === 1) { // $option = Option::get('agency_price_config', 0, 'saas'); // if ($option['value']) { // $option = json_decode($option['value'], true); // } // $model_type = $option['model_type'] ?: 0; $province_id = $this->address['province_id']; $city_id = $this->address['city_id']; $district_id = $this->address['district_id']; $admin = Admin::find() ->where(['is_delete' => 0]) ->andWhere([ 'area_level' => 1, 'province_id' => $province_id, 'city_id' => $city_id, 'district_id' => $district_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT ])->one(); if (empty($admin)) { $admin = Admin::find() ->where(['is_delete' => 0]) ->andWhere([ 'area_level' => 2, 'province_id' => $province_id, 'city_id' => $city_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT ])->one(); if (empty($admin)) { $admin = Admin::find() ->where(['is_delete' => 0]) ->andWhere([ 'area_level' => 3, 'province_id' => $province_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT ])->one(); } } if (!$admin) { $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column(); $district = implode(' ', $district); return [ 'code' => 1, 'msg' => "未找到对应".$district."区域下的代理" ]; } } } } public function getOrderNo() { $order_no = null; while (true) { $order_no = date('YmdHis') . mt_rand(100000, 999999); $exist_order_no = Order::find()->where(['order_no' => $order_no])->exists(); if (!$exist_order_no) { break; } } return $order_no; } /** * @param $mch * @param $order Order * @return array */ private function insertData($mch, $order) { // 计算商品相关 $goods_list = $mch['goods_list']; foreach ($goods_list as $goods) { foreach ($goods['cart'] as $cart) { // 删除购物车 if (isset($cart['cart_id']) && $cart['cart_id'] > 0) { PurchaseCart::updateAll(['is_delete' => 1], ['id' => $cart['cart_id']]); } $order_detail = new PurchaseOrderDetail(); $order_detail->order_id = $order->id; $order_detail->goods_id = $goods['goods']['id']; $order_detail->num = $cart['num']; $order_detail->total_price = $cart['cart_price']; $order_detail->attr = json_encode($cart['attr_list'], JSON_UNESCAPED_UNICODE); $order_detail->pic = $goods['goods']['pic_url']; $order_detail->goods_name = $goods['goods']['name']; $order_detail->send_type = (int)$goods['goods']['send_type']; if (!$order_detail->save()) { return [ 'code' => 1, 'msg' => '订单提交失败,请稍后再重试', 'error' => $order_detail->errors ]; } } } $order->afterOtherDataSave(); return [ 'code' => 0, 'msg' => '' ]; } public function checkAddress($data, $mch_goods = null) { try { $option = Option::get('agency_price_config', 0, 'saas'); if ($option['value']) { $option = json_decode($option['value'], true); } // $model_type = $option['model_type'] ?: 0; if (empty($data['province_id']) || empty($data['city_id']) || empty($data['district_id'])) { throw new \Exception('地址信息为空'); } $province_id = $data['province_id']; $city_id = $data['city_id']; $district_id = $data['district_id']; $message['code'] = 0; $message['msg'] = "success"; if (empty($mch_goods)) { throw new \Exception('商品为空'); } foreach ($mch_goods as $good) { if ((int)$good['goods']['send_type'] === 1) { $admin = Admin::find() ->where(['is_delete' => 0]) ->andWhere([ 'area_level' => 1, 'province_id' => $province_id, 'city_id' => $city_id, 'district_id' => $district_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT ])->one(); if (empty($admin)) { $admin = Admin::find() ->where(['is_delete' => 0]) ->andWhere([ 'area_level' => 2, 'province_id' => $province_id, 'city_id' => $city_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT ])->one(); if (empty($admin)) { $admin = Admin::find() ->where(['is_delete' => 0]) ->andWhere([ 'area_level' => 3, 'province_id' => $province_id, 'type' => Admin::ADMIN_TYPE_FRONT_AGENT ])->one(); } } // if (!$admin) { // $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column(); // $district = implode(' ', $district); // if ((int)$model_type === 0) { // $admin = Admin::find() // ->where(['is_delete' => 0]) // ->andWhere([ // 'area_level' => 2, // 'province_id' => $province_id, // 'city_id' => $city_id // ])->one(); // } else { // $admin = Admin::find() // ->where(['is_delete' => 0]) // ->andWhere([ // 'area_level' => 1, // 'province_id' => $province_id, // 'city_id' => $city_id, // 'district_id' => $district_id // ])->one(); // } if (empty($admin)) { $district = District::find()->where(['id' => [$province_id, $city_id, $district_id]])->select('name')->column(); $district = implode(' ', $district); return [ 'code' => 1, 'msg' => "未找到".$district."区域下的前置仓" ]; } } } debug_log(json_encode($message)); return $message; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function checkStbz($data, $address = null) { try { if (!empty($data)) { foreach ($data as $value) { $goods_url = "/goods/getAttrParams"; $param = [ 'id' => $value['goods']['id'] ]; //请求接口 $domain = (new OptionSetting)->getCloudDomainName(); $goodsInfo = cloud_post($domain . $goods_url, $param); $goodsInfo = json_decode($goodsInfo, true); if ((int)$goodsInfo['code'] !== 0) { throw new \Exception($goodsInfo['msg']); } if ($goodsInfo['data']['stbz_goods_id'] > 0) { if (empty($value['goods']['attrs'])) { throw new \Exception("规格信息错误"); } $attrs = json_decode($value['goods']['attrs'], true); $spu = []; foreach ($value['cart'] as $cart) { $num = $cart['num']; if (empty($cart['attr_list'])) { throw new \Exception("规格信息错误"); } $attr_id = array_column($cart['attr_list'], 'attr_id'); foreach ($attrs as $attr) { $attrs_id = array_column($attr['attr_list'], 'attr_id'); if (!array_diff($attrs_id, $attr_id)) { $no = $attr['no']; $spu[] = [ 'number' => $num, 'sku' => $no, ]; } } } if (empty($address)) { throw new \Exception("地址信息错误"); } $params = [ 'spu' => $spu, 'address' => [ 'consignee' => $address->name, 'phone' => $address->mobile, 'province' => $address->province, 'city' => $address->city, 'area' => $address->district, 'street' => '', 'description' => $address->detail, ] ]; debug_log('胜天半子商品信息' . json_encode($params)); $checkData = \stbz_client()->getApiResponse('post', '/v2/order/beforeCheck', $params); $checkData = \json_decode($checkData, true); debug_log('胜天半子下单检测' . json_encode($checkData)); if ($checkData['code'] != 1) { throw new \Exception($checkData['msg']); } } } } return [ 'code' => 0, 'msg' => '下单成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }