| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace addons\StarChain\api\modules\v1\controllers;
- use addons\StarChain\common\enums\RefundTypeEnum;
- use addons\StarChain\common\enums\ShippingTypeEnum;
- use addons\StarChain\common\enums\StarChainEnum;
- use addons\StarChain\common\models\GoodsAttr;
- use addons\StarChain\common\models\MallOrder;
- use addons\StarChain\common\services\ChainOrderService;
- use addons\StarChain\common\services\RefundService;
- use api\controllers\OnAuthController;
- use common\enums\AppEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\RefundStatusEnum;
- use common\enums\ShippingStatusEnum;
- use common\globals\GlobalInvoke;
- use common\logic\order\OrderFormLogic;
- use common\logic\order\OrderInitDataLogic;
- use common\logic\order\OrderLimitLogic;
- use common\logic\order\OrderMarketingLogic;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderRefund;
- use common\models\user\UserAddress;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * 默认控制器
- *
- * Class OrderController
- * @package addons\StarChain\api\modules\v1\controllers
- */
- class OrderController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 订单预览
- *
- * @return string
- */
- public function actionPreview()
- {
- $orderFormLogic = new OrderFormLogic();
- $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
- $form->attributes = Yii::$app->request->post();
- $form['action'] = AppEnum::ACTION_PREVIEW;
- !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS;
- // 初始化订单数据
- $orderInitDataLogic = new OrderInitDataLogic();
- $form = $orderInitDataLogic->execute($form, $form->type);
- if ($form === false) {
- return $this->error($orderInitDataLogic->getError());
- }
- // 购买限制验证
- $orderLimitLogic = new OrderLimitLogic();
- $form = $orderLimitLogic->run($form);
- if ($form === false) {
- return $this->error($orderLimitLogic->getError());
- }
- // 获取收货地址
- $address_info = UserAddress::getUserAddress($this->user_id, $form->address_id);
- $form->address_id = $address_info['id'] ?? 0;
- // 营销减免活动
- $orderMarketingLogic = new OrderMarketingLogic();
- $form = $orderMarketingLogic->run($form);
- if ($form === false) {
- return $this->error($orderMarketingLogic->getError());
- }
- // 聚合供应链处理
- $service = new ChainOrderService(['mall_id' => $this->mall_id]);
- try {
- $form = $service->createBeforeCheck($form);
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- $group_order_goods = array_values($form['group_order_goods']);
- $marketing = $form['marketing'];
- $pay_type_list = Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform);
- $shipping_type_list = ShippingTypeEnum::getMap();
- $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list');
- $info['total_pay_money'] = '¥' . $form['total_pay_money'];
- $info['is_address'] = $form['is_address'] ?? 0;
- return $this->success('操作成功', $info);
- }
- /**
- * 创建订单
- *
- * @return string
- */
- public function actionCreate()
- {
- $orderFormLogic = new OrderFormLogic();
- $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id);
- $form->attributes = Yii::$app->request->post();
- $form['action'] = AppEnum::ACTION_CREATE;
- // 初始化订单数据
- $orderInitDataLogic = new OrderInitDataLogic();
- $form = $orderInitDataLogic->execute($form, $form->type);
- if ($form === false) {
- return $this->error($orderInitDataLogic->getError());
- }
- // 购买限制验证
- $orderLimitLogic = new OrderLimitLogic();
- $form = $orderLimitLogic->run($form);
- if ($form === false) {
- return $this->error($orderLimitLogic->getError());
- }
- // 营销减免活动
- $orderMarketingLogic = new OrderMarketingLogic();
- $form = $orderMarketingLogic->run($form, true);
- if ($form === false) {
- return $this->error($orderMarketingLogic->getError());
- }
- // 聚合供应链处理
- $service = new ChainOrderService(['mall_id' => $this->mall_id]);
- try {
- $form = $service->createBeforeCheck($form, true);
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- $res = Order::createOrder($form);
- if ($res === false) {
- return $this->error(Order::getStaticError());
- }
- return $this->success('操作成功', $res);
- }
- /**
- * 加载提交售后订单数据
- *
- * @return string
- */
- public function actionToRefundSubmit()
- {
- $params = $this->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['order_detail_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $select = 'id,order_id,goods_name,sign_id,goods_id,goods_attr_name,goods_attr_id,price,num,pic_url,goods_price,order_status,marketing_type,extra_info,adjust_money';
- $order_detail = OrderDetail::getOne([['id' => $params['order_detail_id']], 'user_id' => $this->user_id, 'mall_id' => $this->mall_id], 1, [], false, $select);
- if (empty($order_detail)) return $this->error('订单不存在');
- $order = MallOrder::find()->where(['id' => $order_detail['order_id']])->select('id, order_no, shipping_money, order_source')->one();
- if ($order->order_source != StarChainEnum::PLUGIN_SIGN) return $this->error('该订单不是聚合供应链订单');
- // 公共调用
- $order_detail = GlobalInvoke::exec(GlobalInvoke::ORDER_REFUND_SUBMIT, $this->mall_id, [
- 'order_detail' => $order_detail,
- 'mall_id' => $this->mall_id,
- ]) ?: $order_detail;
- // 价格等于商品价格加上修改价格
- $order_detail['goods_price'] = round($order_detail['goods_price'] + $order_detail['adjust_money'], 2);
- $data['goods_info'] = $order_detail;
- $feedback_type = RefundTypeEnum::getRefundMap();
- $data['feedback_type'] = [];
- if (in_array($order_detail['order_status'], [OrderStatusEnum::PAY])) {
- // 待发货
- $data['feedback_type'][] = $feedback_type[RefundTypeEnum::MONEY];
- }
- if (in_array($order_detail['order_status'], [OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING])) {
- // 已发货,已收货
- // $data['feedback_type'][] = $feedback_type[RefundTypeEnum::ONLY_MONEY];
- $data['feedback_type'][] = $feedback_type[RefundTypeEnum::MONEY_AND_PRODUCT];
- $data['feedback_type'][] = $feedback_type[RefundTypeEnum::EXCHANGE_GOODS];
- $data['feedback_type'][] = $feedback_type[RefundTypeEnum::ONLY_MONEY];
- }
-
- // 待发货添加邮费
- if ($order_detail['order_status'] == OrderStatusEnum::PAY) {
- $data['goods_info']['goods_price'] = bcadd($order_detail['goods_price'], $order->shipping_money, 2);
- }
- $data['order_status'] = $order_detail['order_status'];
- return $this->success('操作成功', $data);
- }
- /**
- * 提交售后数据
- *
- * @return string
- */
- public function actionDoRefundSubmit()
- {
- $params = $this->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['order_detail_id', 'reason_id', 'type', 'remark'], 'required'],
- [['shipping_status', 'pic_list', 'reason', 'refund_price', 'remark', 'type'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $pic_list = Json::decode($params['pic_list']);
- if (count($pic_list) > 4) return $this->error('上传图片不能超过4张');
- $params['user_id'] = $this->user_id;
- $params['mall_id'] = $this->mall_id;
- $where = [
- ['=', 'order_detail_id', $params['order_detail_id']],
- ['=', 'user_id', $this->user_id],
- ['=', 'mall_id', $this->mall_id],
- ['>', 'step_status', 0],
- ['<>', 'refund_status', RefundStatusEnum::REVOKE]
- ];
- $order_refund_model = OrderRefund::getOne($where, true);
- if ($order_refund_model) return $this->error('该订单商品已经申请过售后');
- $refund_service = new RefundService(['mall_id' => $this->mall_id]);
- $res = $refund_service->doRefund($params);
- if ($res === false) return $this->error($refund_service->error);
- return $this->success('操作成功', ['id' => $res->id]);
- }
- /**
- * 聚合供应链售后上传售后截图
- *
- * @return void
- */
- public function actionUpload()
- {
- // 走主商城
- return $this->error('功能开发中');
- }
- /**
- * 填写物流单号
- *
- * @return void
- */
- public function actionRefundSend()
- {
- // 走主商城
- return $this->error('功能开发中');
- }
- /**
- * 物流编号
- *
- * @return void
- */
- public function actionExpressList()
- {
- // 走主商城
- return $this->error('功能开发中');
- }
- }
|