| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\AgentFrontBind;
- use app\models\AgentFrontCentralizeGoods;
- use app\models\AgentFrontCentralizeGoodsRefundLog;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\OrderRefund;
- use app\models\OrderTransit;
- use app\modules\admin\models\MerchantForm;
- use yii\base\Model;
- use yii\helpers\Json;
- class OrderRefundSendForm extends Model
- {
- public $order_refund_id;
- public $user_id;
- public $express;
- public $express_no;
- public $orderType;
- public $form_id;
- public $send_type;//发货方式:0快递 1上门取货 2退回门店
- public $latitude;
- public $longitude;
- public $name;
- public $mobile;
- public $province_id;
- public $city_id;
- public $district_id;
- public $address;
- public $main_goods_pic;
- public $other_goods_pic;
- public $back_md_pic;
- public function rules()
- {
- return [
- [['express', 'express_no', 'orderType', 'form_id'], 'trim'],
- [['order_refund_id'], 'required'],
- [['express', 'express_no'], 'string'],
- [['send_type', 'name', 'mobile', 'main_goods_pic', 'other_goods_pic', 'back_md_pic', 'province_id', 'city_id', 'district_id', 'address'], 'safe'],
- [['latitude', 'longitude'], 'number']
- ];
- }
- public function attributeLabels()
- {
- return [
- 'express' => '快递公司',
- 'express_no' => '快递单号',
- ];
- }
- public function save()
- {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- }
- $send_type = intval($this->send_type);
- $query = OrderRefund::find();
- $order_refund = $query->where([
- 'id' => $this->order_refund_id,
- 'user_id' => $this->user_id,
- ])->one();
- if (!$order_refund) {
- return [
- 'code' => 1,
- 'msg' => '售后订单不存在。',
- ];
- }
- $order = Order::findOne($order_refund->order_id);
- $md_id = 0;
- if ($order) {
- $md_id = $order->md_id;
- }
- $agentFrontBind = AgentFrontBind::findOne(['md_id' => $md_id, 'is_delete' => 0]);
- $goods_list = OrderDetail::find()->where(['order_id' => $order_refund->order_id])->andWhere(['in', 'id', Json::decode($order_refund->order_detail_id)])
- ->select('attr, goods_info, num')->asArray()->all();
- $is_front_centralize = 0;
- $centralize_goods_id_arr = [];
- $goods_num_info = [];
- foreach ($goods_list as &$goods) {
- $goods_info = json_decode($goods['goods_info'], true);
- // if (intval($goods_info['is_front_centralize']) && $send_type) {
- //
- // }
- if (intval($goods_info['is_front_centralize'])) {
- $is_front_centralize = 1;
- if (!$send_type) {
- return [
- 'code' => 1,
- 'msg' => '部分商品售后发货仅支持退回门店以及上门取送。',
- ];
- }
- $detail_attr = json_decode($goods['attr'], true) ?? [];
- $detail_attr_id = array_column($detail_attr, 'attr_id');
- // var_dump($goods['attr']);die;
- sort($detail_attr_id);
- $goods_attr = json_decode($goods_info['attr'], true);
- $goods_no = '';
- foreach ($goods_attr as $goods_attr_item) {
- $goods_attr_id = array_column($goods_attr_item['attr_list'], 'attr_id');
- sort($goods_attr_id);
- if (!array_diff($goods_attr_id, $detail_attr_id)) {
- $goods_no = $goods_attr_item['no'];
- }
- }
- $agentFrontCentralizeGoods = AgentFrontCentralizeGoods::findOne([
- 'front_admin_id' => $agentFrontBind->front_agent_admin_id,
- 'goods_no' => $goods_no,
- 'is_delete' => 0
- ]);
- if ($agentFrontCentralizeGoods) {
- $goods_num_info[] = [
- 'num' => $goods['num'],
- 'centralize_goods_id' => $agentFrontCentralizeGoods->id,
- ];
- array_push($centralize_goods_id_arr, $agentFrontCentralizeGoods->id);
- }
- }
- if (in_array($send_type, [1, 2]) && !intval($goods_info['is_front_centralize'])) {
- return [
- 'code' => 1,
- 'msg' => '部分商品售后发货仅支持快递。',
- ];
- }
- }
- if ($is_front_centralize) {
- $goodsRefundLog = AgentFrontCentralizeGoodsRefundLog::findOne(['refund_id' => $order_refund->id]) ?: new AgentFrontCentralizeGoodsRefundLog();
- $goodsRefundLog->refund_type = $send_type;
- $goodsRefundLog->centralize_goods_id = implode(',', $centralize_goods_id_arr ?? []);
- $goodsRefundLog->md_id = $md_id;
- $goodsRefundLog->store_id = $order->store_id;
- $goodsRefundLog->refund_id = $order_refund->id;
- $goodsRefundLog->goods_num_info = json_encode($goods_num_info);
- if ($send_type == AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_VISIT) {
- if (empty($this->name) ||
- empty($this->mobile) || empty($this->province_id) || empty($this->city_id) || empty($this->district_id)
- || empty($this->address)
- // || empty($this->latitude) || empty($this->longitude)
- ) {
- return [
- 'code' => 1,
- 'msg' => '上门取货请填写寄件人信息。',
- ];
- }
- $goodsRefundLog->latitude = $this->latitude ?? 0;
- $goodsRefundLog->longitude = $this->longitude ?? 0;
- $goodsRefundLog->name = $this->name;
- $goodsRefundLog->mobile = $this->mobile;
- $goodsRefundLog->province_id = $this->province_id;
- $goodsRefundLog->city_id = $this->city_id;
- $goodsRefundLog->district_id = $this->district_id;
- $goodsRefundLog->address = $this->address;
- $this->express = "仓库集采-上门取货";
- $this->express_no = "-";
- }
- if ($send_type == AgentFrontCentralizeGoodsRefundLog::REFUND_TYPE_BACK_MD) {
- // if (empty($this->back_md_pic)) {
- // return [
- // 'code' => 1,
- // 'msg' => '退回门店请上传 送回门店照片。',
- // ];
- // }
- $goodsRefundLog->back_md_pic = $this->back_md_pic;
- $this->express = "仓库集采-退回门店";
- $this->express_no = "-";
- }
- if (!$goodsRefundLog->save()) {
- return [
- 'code' => 1,
- 'msg' => implode(';', array_values($order->firstErrors))
- ];
- };
- }
- $order_refund->is_user_send = 1;
- $order_refund->user_send_express = $this->express;
- $order_refund->user_send_express_no = $this->express_no;
- $orderTransit = OrderTransit::findOne(['order_id' => $order_refund->order_id, 'is_delete' => 0, 'cancel_examine' => 1]);
- if ($orderTransit) {
- $cloud_order_id = $orderTransit->cloud_order_id;
- $merchant = new MerchantForm();
- $merchant->order_id = $cloud_order_id;
- $merchant->user_send_express = $this->express;
- $merchant->user_send_express_no = $this->express_no;
- $merchant->order_type = 0;
- $merchant->pic_list = [];
- $r = $merchant->mchOrderRefundSend($order_refund->order_detail_id);
- if ($r['code']) {
- return $r;
- }
- }
- if ($order_refund->save()) {
- return [
- 'code' => 0,
- 'msg' => '发货成功。',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $order_refund->errors[0]
- ];
- }
- }
- }
|