| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\purchase\order;
- use app\constants\OptionSetting;
- use app\models\Address;
- use app\models\Admin;
- use app\models\Cart;
- use app\models\common\CommonOrder;
- use app\models\DeliveryInfo;
- use app\models\District;
- use app\models\FoodCart;
- use app\models\FoodFlag;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\Md;
- use app\models\MdProfit;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\Register;
- use app\models\Store;
- use app\models\Supplier;
- use app\models\User;
- use app\models\UserCoupon;
- use app\models\VerifyCard;
- use app\models\VerifyCardSale;
- use app\models\VerifyCardLog;
- use app\plugins\adopt\models\AdoptGoods;
- use app\plugins\adopt\models\AdoptOrderInfo;
- use app\utils\CloudPrint;
- use app\utils\Delivery\Delivery;
- use app\utils\Notice\NoticeSend;
- use app\utils\OrderNo;
- use app\utils\PrintOrder;
- use app\utils\Tools;
- use yii\helpers\Json;
- use app\modules\alliance\models\purchase\order\OrderSubmitPreviewForm;
- use app\models\PurchaseOrder;
- use app\models\PurchaseOrderDetail;
- use app\models\PurchaseCart;
- class OrderSubmitForm extends OrderForm
- {
- public $user;
- public $payment;
- public $cart_id_list;
- public $attr_list;
- public $goods_id;
- public $remark;
- public function rules()
- {
- $rules = [
- [['payment'], 'integer'],
- ['address_id', 'integer'],
- [['cart_id_list', 'attr_list', 'goods_id', 'remark'], 'safe'],
- ];
- return $rules;
- }
- public function afterValidate() {
- $this->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()
- ];
- }
- }
- }
|