| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- //退款退货
- namespace hd\controllers;
- use biz\shop\classes\ShopExtClass;
- use bizHd\custom\classes\CustomClass;
- use bizHd\hb\classes\HbClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\order\classes\OrderGoodsClass;
- use bizHd\order\classes\OrderItemClass;
- use bizHd\order\services\OrderService;
- use bizHd\refund\classes\HdRefundClass;
- use bizHd\refund\classes\HdRefundGoodsClass;
- use bizHd\refund\classes\HdRefundItemClass;
- use bizHd\refund\services\HdRefundService;
- use common\components\noticeUtil;
- use Yii;
- use common\components\util;
- class RefundController extends BaseController
- {
- //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
- public function actionRefundAmount()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? '';
- $order = OrderClass::getById($id, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->mainId != $this->mainId) {
- util::fail('不是你的订单');
- }
- $staff = $this->shopAdmin;
- if ($staff->mobile != '15280215347') {
- util::fail('请联系管理员操作');
- }
- $shop = $this->shop;
- $respond = HdRefundClass::onlyRefundAmount($shop, $order);
- $status = $respond['status'];
- $msg = $respond['msg'];
- util::success(['status' => $status], $msg);
- }
- //列表
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $this->mainId;
- $orderSn = $get['orderSn'] ?? '';
- if (!empty($orderSn)) {
- $where['orderSn'] = $orderSn;
- }
- $list = HdRefundClass::getOrderList($where);
- util::success($list);
- }
- //退款单详情 ssh 20220427
- public function actionDetail()
- {
- $id = Yii::$app->request->get('id', 0);
- $refund = HdRefundClass::getById($id, true);
- HdRefundClass::valid($refund, $this->mainId);
- $refundSn = $refund->refundSn ?? '';
- $itemList = HdRefundItemClass::getItemListBySn($refundSn);
- $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
- util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
- }
- //退款 ssh 20220427
- public function actionCreateOrder()
- {
- $shopAdmin = $this->shopAdmin;
- if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
- util::fail('超管才能操作退款');
- }
- $post = Yii::$app->request->post();
- $id = isset($post['id']) ? $post['id'] : 0;
- $version = $post['version'] ?? 1;
- if ($version == 1) {
- util::fail('请升级小程序');
- }
- $refundType = $post['refundType'] ?? 1;//1退货并退款2只退款
- //避免重复提交
- util::checkRepeatCommit($id, 10);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $order = OrderClass::getById($id, true);
- OrderClass::valid($order, $this->mainId);
- if ($order->payStatus == 0) {
- util::fail('订单没有付款');
- }
- if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
- util::fail("待付款订单无法退款");
- }
- if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
- util::fail("取消订单不能发起退款");
- }
- if ($order->fromType == 4) {
- util::fail('请在美团APP发起退货退款');
- }
- if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
- util::fail('2023年10月17日前订单无法申请退款');
- }
- if ($order->status != 4) {
- //util::fail('订单完成了才能售后');
- }
- $addTime = $order->addTime ?? '';
- $current = time();
- if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
- util::fail('历史订单,不能再发起退款');
- }
- if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
- util::fail('历史订单,不能发起退款哦');
- }
- if ($order->forward == 1) {
- util::fail('负数订单不能发起售后');
- }
- if ($refundType == 1) {
- //商品花材结构 property 0花束 1花材
- //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
- $productJson = $post['product'] ?? '';
- if (empty($productJson)) {
- util::fail('请选择商品');
- }
- $productList = json_decode($productJson, true);
- if (!is_array($productList)) {
- util::fail('请选择商品哦');
- }
- $post['product'] = $productList;
- }
- $post['price'] = $post['price'] ?? 0;
- if ($post['price'] <= 0) {
- util::fail("退款金额不能小于0");
- }
- if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
- util::fail("退款金额超过订单金额");
- }
- $post['shopId'] = $this->shopId;
- $post['sjId'] = $this->sjId;
- $post['shopAdminId'] = $this->shopAdminId;
- $post['mainId'] = $this->mainId;
- $shopAdmin = $this->shopAdmin;
- $adminName = $shopAdmin['name'] ?? '';
- $post['shopAdminName'] = $adminName;
- $main = $this->main;
- //判断是否开负数订单
- $ret = OrderClass::ifShPay($order, $main, $post['price']);
- $shAddPay = $ret['shAddPay'] ?? 0;
- if ($shAddPay == 1) {
- $customId = $order->customId ?? 0;
- $custom = CustomClass::getById($customId, true);
- if (empty($custom)) {
- util::fail('没有客户哈!');
- }
- //线下付款
- $hasPay = 1;
- $shopId = $order->shopId;
- $staffId = $this->shopAdminId;
- $staffName = $this->shopAdminName;
- $forwardStock = $refundType == 1 ? 0 : 1;
- $refundPrice = $post['price'] ?? 0;
- $hasItem = false;
- $hasBouquet = false;
- // property 0花束 1花材
- $orderProductList = [];
- foreach ($productList as $thisInfo) {
- $num = $thisInfo['num'] ?? 0;
- $unitType = $thisInfo['unitType'] ?? 0;
- $property = $thisInfo['property'] ?? 0;
- $unitPrice = $thisInfo['unitPrice'] ?? 0;
- $productId = $thisInfo['productId'] ?? 0;
- $orderProductList[] = [
- 'productId' => $productId,
- 'unitPrice' => $unitPrice,
- 'remark' => '',
- 'property' => $property,
- 'unitType' => $unitType,
- 'num' => $num,
- ];
- if ($property == 0) {
- $hasBouquet = true;
- }
- if ($property == 1) {
- $hasItem = true;
- }
- }
- $orderType = 0;
- if ($hasBouquet) {
- $orderType = 1;
- }
- if ($hasItem) {
- $orderType = 2;
- }
- if ($hasItem && $hasBouquet) {
- $orderType = 3;
- }
- $sjId = $order->sjId;
- $mainId = $order->mainId;
- $deadline = time() + 8640000;
- $hdId = $order->hdId;
- $hdName = $order->hdName;
- $customNamePy = $order->customNamePy;
- $customName = $order->customName;
- $orderData = [
- 'hasPay' => 1,
- 'payWay' => 0,
- 'shopId' => $shopId,
- 'customId' => $customId,
- 'customName' => $customName,
- 'customNamePy' => $customNamePy,
- 'product' => $orderProductList,
- 'needPrint' => 2,
- 'getStaffId' => $staffId,
- 'getStaffName' => $staffName,
- 'shopAdminId' => $staffId,
- 'shopAdminName' => $staffName,
- 'dealPrice' => 1,
- 'forward' => 1,
- 'forwardStock' => $forwardStock,
- 'modifyPrice' => $refundPrice,
- 'orderType' => $orderType,
- 'sjId' => $sjId,
- 'mainId' => $mainId,
- 'deadline' => $deadline,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'flowerNum' => 0,
- 'fromType' => 1,
- 'hdId' => $hdId,
- 'hdName' => $hdName,
- 'sendType' => 1,
- 'sendSide' => 2,
- 'labourCost' => 0,
- ];
- //走负数订单,多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
- $returnOrder = OrderService::createHdOrder($orderData, $custom, $hasPay);
- $shop = $this->shop;
- $staff = $this->shopAdmin;
- //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
- OrderClass::shPayChange($returnOrder, $shop, $staff, $order);
- $transaction->commit();
- $orderId = $returnOrder->id;
- util::success(['forward' => 1, 'orderId' => $orderId]);
- } else {
- //走正常售后
- HdRefundService::addRefund($post, $order);
- }
- $transaction->commit();
- //红包退款
- $refundHb = $post['refundHb'] ?? 0;
- $hbId = $order->hbId;
- if ($refundHb == 1 && $hbId != 0 && bccomp($post['price'], $order->orderPrice, 2) == 0) {
- $hb = HbClass::getById($hbId, true);
- if (empty($hb)) {
- util::fail('没有找到红包');
- }
- $hb->status = 0;
- $hb->orderId = 0;
- $hb->save();
- }
- //制作单取消通知
- $shopId = $this->shopId ?? 0;
- $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
- ShopExtClass::orderCancelRemind($shopExt, $order);
- util::complete();
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info("出错了:" . $exception->getMessage());
- util::fail('操作失败');
- }
- util::complete();
- }
- public function actionAllRefund()
- {
- util::fail('功能开发中');
- }
- }
|