store_id = get_saas_purchase_store_id(); parent::init(); } public function rules() { $rules = [ ['address_id', 'integer'], ['verify_card_id', 'integer'], ['order_type', 'integer'], [['order_type', 'delete_pass', 'saas_id'], 'default', 'value' => 0], [['longitude', 'latitude', '_from'], 'trim'], [['_from'], 'in', 'range' => ['app', 'mini']], [['mch_list', 'express', 'express_no', 'words', 'apply_data'], 'safe'], ]; return $rules; } public function afterValidate() { $this->user = User::findOne($this->user_id); // $this->level = $this->getLevelData(); $this->address = $this->getAddressData(); $this->integral = [ 'forehead' => 0, 'forehead_integral' => 0, 'integration' => Option::get(OptionSetting::STORE_INTEGRATION, $this->store_id, 'gift', Option::get(OptionSetting::STORE_INTEGRATION, $this->store_id, 'store')['value'])['value'] ]; parent::afterValidate(); } protected function getAddress() { if (!$this->address) { if ($this->address_id) { $this->address = Address::findOne(['id' => $this->address_id, 'user_id' => get_saas_user_id(), 'is_delete' => 0]); } else { $this->address = Address::find()->where([ 'user_id' => get_saas_user_id(), 'is_default' => 1, 'is_delete' => 0, ])->limit(1)->one(); } } return (object)$this->address; } //获取收货地址,有address_id优先获取,没有则获取默认地址 protected function getAddressData() { $address = $this->getAddress(); if (isset($address->id)) { return [ 'id' => $address->id, 'name' => $address->name, 'mobile' => $address->mobile, 'province_id' => $address->province_id, 'province' => $address->province, 'city_id' => $address->city_id, 'city' => $address->city, 'district_id' => $address->district_id, 'district' => $address->district, 'detail' => $address->detail, 'is_default' => $address->is_default, 'latitude' => $address->latitude, 'longitude' => $address->longitude, ]; } else { return null; } } //获取支付方式 public function getPayTypeList() { $pay_type_list_json = Option::get(OptionSetting::STORE_PAYMENT, $this->store_id, 'pay', Option::get(OptionSetting::STORE_PAYMENT, $this->store_id, 'store', '{"wechat":{"value":1}}')['value']); $pay_type_list = Json::decode($pay_type_list_json['value']); $new_list = []; $ok = true; $is_virtual = false; foreach ($this->mch_list as $mch) { if (!empty($mch['goods_list'][0]['product_type']) && $mch['goods_list'][0]['product_type'] == 3) { $is_virtual = true; } if ($mch['mch_id'] == 0) { continue; } else { $ok = false; break; } } if (is_wechat_platform() || $this->_from == 'app') { $new_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } if (is_alipay_platform() || $this->_from == 'app') { $new_list[] = [ 'name' => '支付宝支付', 'payment' => 4, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-alipay.png' ]; } if (is_toutiao_platform()) { $new_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png' ]; } return $new_list; } /** * 支付方式 * @param array $is_payment //支付方式 * @param array $ignore //忽略的支付方式 * @return array */ public static function getPayType($is_payment = array(), $ignore = array()) { if (!$is_payment || empty($is_payment)) { $default = '{"wechat":{"value":1}}'; if (is_alipay_platform()) { $default = '{"alipay":{"value":1}}'; } if (is_toutiao_platform()) { $default = '{"toutiao":{"value":1}}'; } $pay_str = Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'pay', Option::get(OptionSetting::STORE_PAYMENT, get_store_id(), 'store', $default)['value']); $is_payment = Json::decode($pay_str['value']); } $pay_type_list = []; foreach ($is_payment as $index => $value) { if (in_array($index, $ignore)) { continue; } if ($index == 'wechat' && $value['value'] == 1 && is_wechat_platform()) { $pay_type_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png' ]; } if ($index == 'toutiao' && $value['value'] == 1 && is_toutiao_platform()) { $pay_type_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-online.png' ]; } if ($index == 'huodao' && $value['value'] == 1) { $pay_type_list[] = [ 'name' => '货到付款', 'payment' => 2, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-huodao.png' ]; } if (get_params('_from') == OrderPayDataForm::PAY_FROM_APP || is_alipay_platform()) { if ($index == 'alipay' && $value['value'] == 1) { $pay_type_list[] = [ 'name' => '支付宝支付', 'payment' => 4, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/recharge/icon-alipay.png' ]; } } if ($index == 'balance' && $value['value'] == 1) { $pay_type_list[] = [ 'name' => '账户余额支付', 'payment' => 3, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/wxapp/images/icon-payment-balance.png' ]; } } if (!$pay_type_list) { if (is_wechat_platform()) { $pay_type_list[] = [ 'name' => '微信支付', 'payment' => 1, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png' ]; } if (is_toutiao_platform()) { $pay_type_list[] = [ 'name' => '线上支付', 'payment' => 5, 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/v1/statics/images/recharge/icon-online.png' ]; } } return $pay_type_list; } /** * 订单取消 * @return \yii\web\Response */ public function revoke($order_id) { $t = Yii::$app->db->beginTransaction(); $order = PurchaseOrder::findOne(['id' => $order_id, 'saas_id'=> $this->saas_id, 'is_delete' => 0]); if (!$order) { return [ 'code' => 1, 'msg' => '订单不存在' ]; } if ($order->cloud_order_id <= 0) { $order->trade_status = 1; if ($order->is_pay == 1) { if ($order->pay_type == 1) { $res = \app\utils\Wechat\WechatBusinessPay::orderRefund($order, OrderNo::ORDER_ALLIANCE_PURCHASE, $order->pay_price, $order->order_no); if ($res['code'] === 1) { return $res; } } } $t->commit(); if (!$order->save()) { return [ 'code' => 1, 'msg' => implode(';', array_values($order->firstErrors)) ]; } return [ 'code' => 0, 'msg' => "操作成功" ]; } // 已支付订单需要后台先审核 if ($order->is_pay == 1 && !$this->delete_pass) { $order->apply_delete = PurchaseOrder::ORDER_APPLY_DELETE; if ($order->save()) { if($order->cloud_order_id){ $form = new MerchantForm(); $form->order_id = $order->cloud_order_id; $form->setSaasStoreCloudId(); $iCancel = $form->mchOrderCancel(); if($iCancel['code'] != 0){ $t->rollBack(); return $iCancel; } } $t->commit(); //NoticeSend::MchCancel($order->order_no, $order->mch_id); return [ 'code' => 0, 'msg' => '订单取消申请已提交,请等候管理员审核' ]; } else { $t->rollBack(); return ['code' => 1, 'msg' => $order->errors[0]]; } } $t->commit(); return $this->revokeApply($order); } /** * 订单取消 * @return \yii\web\Response */ public function revokeApply($order) { $t = Yii::$app->db->beginTransaction(); if(get_admin() && !$this->delete_pass){ $order->apply_delete = Order::ORDER_APPLY_DELETE_DEFAULT; $order->remark = $this->remark; if(!$order->save()){ $t->rollBack(); return [ 'code' => 1, 'msg' => '操作失败,数据保存失败', ]; } if($order->cloud_order_id){ $form = new SupplierForm(); $form->order_id = (string)$order->cloud_order_id; $form->cancel_type = 2; $form->token_supplier_id = $order->supplier_id; $iCancel = $form->supplierOrderCancel(); if($iCancel['code'] != 0){ $t->rollBack(); return $iCancel; } } $t->commit(); // TODO :推送订单取消拒绝消息 return [ 'code' => 0, 'msg' => '操作成功,拒绝取消', ]; } $order->trade_status = PurchaseOrder::ORDER_FLOW_CANCEL; $order->remark = $this->remark; // $order->is_delete = Order::IS_DELETE_TRUE; if ($order->save()) { // 订单退款 $form = new SupplierForm(); $form->order_id = (string)$order->cloud_order_id; $form->cancel_type = 1; $form->token_supplier_id = $order->supplier_id; $iCancel = $form->supplierOrderCancel(); if($iCancel['code'] != 0){ $t->rollBack(); return $iCancel; } if ($order->is_pay == 1) { if ($order->pay_type == 1) { $refundFee = $order->pay_price; $orderRevoke = new OrderRevoke(); $orderRevoke->order = $order; $orderRevoke->type = 0; $res = $orderRevoke->orderWxRefund($refundFee); if ($res !== true) { $t->rollBack(); return $res; } } } $t->commit(); return [ 'code' => 0, 'msg' => '订单已取消' ]; } $t->rollBack(); return [ 'code' => 1, 'msg' => '订单取消失败' ]; } public function send() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $order = PurchaseOrder::findOne([ 'is_delete' => 0, 'id' => $this->order_id, ]); if (!$order) { return [ 'code' => 1, 'msg' => '订单不存在或已删除', ]; } if ($order->is_pay == 0) { return [ 'code' => 1, 'msg' => '订单未支付' ]; } if ($order->apply_delete == PurchaseOrder::ORDER_APPLY_DELETE) { return [ 'code' => 1, 'msg' => '该订单正在申请取消操作,请先处理' ]; } // $expressList = Express::getExpressList(); // $ok = false; // foreach ($expressList as $value) { // if ($value['name'] == $this->express) { // $ok = true; // break; // } // } // if (!$ok) { // return [ // 'code' => 1, // 'msg' => '快递公司不正确' // ]; // } $order->express = $this->express; $order->express_no = $this->express_no; $order->words = $this->words; $order->trade_status = PurchaseOrder::ORDER_FLOW_SEND; $order->send_time = time(); if ($order->save()) { // $goods = Goods::findOne(OrderDetail::findOne(['order_id' => $order->id])->goods_id); // NoticeSend::OrderSend($order->user_id, $order->mobile, $order->order_no, $goods->name, $this->express, $this->express_no); return [ 'code' => 0, 'msg' => '发货成功', ]; } else { return [ 'code' => 1, 'msg' => '操作失败', ]; } } /** * 订单确认收货 */ public function confirm() { $t = Yii::$app->db->beginTransaction(); $order = PurchaseOrder::findOne([ 'saas_id' => get_saas_user_id(), 'id' => $this->order_id, 'trade_status' => PurchaseOrder::ORDER_FLOW_SEND, 'is_delete' => 0, ]); if (!$order) { return [ 'code' => 1, 'msg' => '订单不存在' ]; } //如果是代理配送 且上门安装,用户没有对商品确认收货则不可确认收货 $order_detail = PurchaseOrderDetail::findAll(['order_id' => $order->id]); foreach ($order_detail as $detail) { $is_can_confirm = ((bool)AgentGoodsInstallLog::findOne(['order_detail_id' => $detail->id, 'status' => [0, 1], 'is_need_install' => 1, 'order_type' => AgentGoodsInstallLog::ORDER_TYPE_PURCHASE])); if ($is_can_confirm) { return [ 'code' => 1, 'msg' => '当前有商品需要上门安装,不可优先确认收货' ]; } } $order->trade_status = PurchaseOrder::ORDER_FLOW_CONFIRM; $order->confirm_time = time(); if ($order->save()) { $afterConfirm = $this->afterConfirm($order); if($afterConfirm['code'] != 0){ $t->rollBack(); return $afterConfirm; } $form = new MerchantForm(); $form->setSaasStoreCloudId(); $form->order_id = $order->cloud_order_id; $confirm = $form->mchOrderConfirm(); if($confirm['code'] != 0){ $t->rollBack(); return $confirm; } $t->commit(); return [ 'code' => 0, 'msg' => '已确认收货', ]; } else { $t->commit(); return [ 'code' => 1, 'msg' => '确认收货失败' ]; } } public function afterConfirm($order){ //判断商品是否存在?存在自动加库存,不存在选品+库存 $res = [ 'code' => 0, 'msg' => 'ok', ]; try { foreach ($order->detail as $od) { $this->_importNum($order, $od); } } catch (\Exception $e) { \Yii::error([__METHOD__, $order, $e]); // return [ // 'code' => 1, // 'msg' => '库存更新失败' . $e->getMessage(), // ]; } return $res; } //判断商品是否存在?存在自动加库存,不存在选品+库存 public function _importNum($order, $od) { $pGoodsId = $od->goods_id; $goods = Goods::find()->where(['is_delete' => 0, 'store_id'=>$order->store_id, 'cloud_goods_id' => $pGoodsId, 'status' => 1, 'is_wholesale' => 1])->orderBy('id DESC')->one(); if(!$goods){ $goods = Goods::find()->where(['is_delete' => 0, 'store_id'=>$order->store_id, 'cloud_goods_id' => $pGoodsId, 'is_wholesale' => 1])->orderBy('id DESC')->one(); } //导入商品 if(!$goods){ $form = new GoodsForm(); $form->store_id = $order->store_id; $form->id = $od->goods_id; $form->pay_price = $od->total_price; $form->is_wholesale = 1; $form->wholesale_num = $od->num; $form->wholesale_attr = $od->attr; $goodsInfo = $form->saveCloudGoods(); if($goodsInfo['_goods']){ $goods = $goodsInfo['_goods']; //库存归零 $attrs = json_decode($goods->attr, true); foreach ($attrs as &$attr) { $attr['num'] = 0; } $goods->attr = json_encode($attrs); $goods->goods_num = 0; $save = $goods->save(); if(!$save){ \Yii::error([__METHOD__, $order, $od, $goods]); } }else{ \Yii::error([__METHOD__, $order, $od, $goodsInfo]); } } if($goods){ $rate = 0; $content = Store::find()->where(['id' => $this->store_id])->select('rate')->asArray()->one(); if ($content) { $rate = $content['rate']; } //修改库存 $odAttrStr = []; $odAttr = json_decode($od->attr, true); foreach($odAttr as $iattr){ $odAttrStr[] = $iattr['attr_name']; } $odAttrStr = implode(',', $odAttrStr); $attrs = json_decode($goods->attr, true); foreach ($attrs as &$attr) { $gAttrStr = []; foreach($attr['attr_list'] as $iattr){ $gAttrStr[] = $iattr['attr_name']; } $gAttrStr = implode(',', $gAttrStr); \Yii::error([__METHOD__, $order, $od, $goods, $gAttrStr, $odAttrStr]); if($gAttrStr == $odAttrStr){ $attr['num'] = intval($attr['num']) + $od->num; $attr['price'] = ($od->total_price + ($od->total_price * ($rate / 100))); $attr = array_merge($attr, [ 'wholesale_num' => (int)$attr['wholesale_num'] += $od->num, 'is_wholesale' => 1 ]); } } $goods->is_wholesale = 1; $goods->attr = json_encode($attrs); // $goods->goods_num = intval($goods->goods_num) + $od->num; $save = $goods->save(); if(!$save){ \Yii::error([__METHOD__, $order, $od, $goods]); } } } }