| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- <?php
- //退款退货
- namespace ghs\controllers;
- use bizGhs\notify\classes\NotifyClass;
- use bizGhs\order\classes\RefundOrderItemClass;
- use bizGhs\order\services\RefundOrderService;
- use bizGhs\shop\classes\ShopClass;
- use bizHd\cg\classes\CgRefundClass;
- use bizHd\cg\classes\CgRefundItemClass;
- use bizHd\notice\classes\NoticeClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\stringUtil;
- use Yii;
- use bizGhs\order\classes\RefundOrderClass;
- use common\components\util;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\services\OrderService;
- use bizGhs\order\classes\StockWastageOrderClass;
- class RefundController extends BaseController
- {
- //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
- public function actionRefundMoney()
- {
- $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('不是你的订单');
- }
- $addTime = $order->addTime;
- $addTime = strtotime($addTime);
- $earTime = strtotime('2025-05-01 00:00:00');
- if ($addTime < $earTime) {
- util::fail('2025.5.1号前的订单,不能退款');
- }
- $shop = $this->shop;
- $purchaseId = $order->purchaseId ?? 0;
- $cg = PurchaseClass::getById($purchaseId, true);
- if (empty($cg)) {
- util::fail('没有找到订单');
- }
- $staff = $this->shopAdmin;
- if ($staff->mobile != '15280215347') {
- util::fail('请联系管理员操作');
- }
- $respond = CgRefundClass::onlyRefundMoney($shop, $cg);
- $status = $respond['status'];
- $msg = $respond['msg'];
- util::success(['status' => $status], $msg);
- }
- //修改金额 ssh 20240412
- public function actionModifyAmount()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $amount = $get['amount'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- if (is_numeric($amount) == false) {
- util::fail('请填写金额');
- }
- if ($refund->status != 0) {
- util::fail('待审核订单才能修改');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $refund->refundPrice = $amount;
- $refund->save();
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- if (empty($cgRefund)) {
- util::fail('没有找到退款信息');
- }
- $cgRefund->refundPrice = $amount;
- $cgRefund->save();
- $transaction->commit();
- util::complete();
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("操作失败原因:" . $e->getMessage());
- util::fail('操作失败');
- }
- }
- //修改申请原因 ssh 20240302
- public function actionModifyCause()
- {
- $staff = $this->shopAdmin;
- if (isset($staff->finance) == false || $staff->finance == 0) {
- util::fail('请有财务权限的人修改');
- }
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $cause = $get['cause'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $refund->cause = $cause;
- $refund->save();
- $orderSn = $refund->orderSn ?? '';
- RefundOrderItemClass::updateByCondition(['orderSn' => $orderSn], ['cause' => $cause]);
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- if (empty($cgRefund)) {
- util::fail('没有找到退款信息');
- }
- $cgRefund->cause = $cause;
- $cgRefund->save();
- $cgOrderSn = $cgRefund->orderSn ?? '';
- CgRefundItemClass::updateByCondition(['orderSn' => $cgOrderSn], ['cause' => $cause]);
- util::complete('修改成功');
- }
- //审核通过 ssh 20230811
- public function actionPass()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('无法操作');
- }
- //多处要同步修改ghs_refund ssh 20250331
- if (getenv('YII_ENV') == 'production') {
- //小向花卉开单售后控制
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
- util::fail('你不能售后哦');
- }
- //小向花卉售后必须备注
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- //小齐出车指定人可以售后
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。');
- }
- }
- }
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $version = $get['version'] ?? 0;
- $confirm = $get['confirm'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $relateOrderSn = $refund->relateOrderSn ?? '';
- if (empty($relateOrderSn)) {
- util::fail('没有订单信息');
- }
- $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
- $has = false;
- if (!empty($refundList)) {
- foreach ($refundList as $single) {
- if ($single->status == 1) {
- $has = true;
- }
- }
- }
- if ($has == true) {
- if ($version == 0) {
- util::fail('请先升级系统');
- }
- if ($confirm == 0) {
- util::success(['error' => 'notFirstApply']);
- }
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- RefundOrderService::passRefund($refund);
- $transaction->commit();
- $staff = $this->shopAdmin;
- $staffId = $staff->id;
- $staffName = $staff->name ?? '';
- $refund->shopAdminId = $staffId;
- $refund->shopAdminName = $staffName;
- $refund->save();
- //审核结果通知
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- $cgShopId = $cgRefund->shopId ?? 0;
- $cgShop = ShopClass::getById($cgShopId, true);
- if (!empty($cgRefund) && !empty($cgShop)) {
- NoticeClass::afterSaleNotice($cgShop, $cgRefund);
- }
- util::complete('操作成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("操作失败原因:" . $e->getMessage());
- util::fail('操作失败');
- }
- }
- //驳回 ssh 20230813
- public function actionReject()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('无法操作');
- }
- //多处要同步修改ghs_refund ssh 20250331
- if (getenv('YII_ENV') == 'production') {
- //小向花卉开单售后控制
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
- util::fail('你不能售后哦。。');
- }
- //小向花卉售后必须备注
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- //小齐出车指定人可以售后
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。。');
- }
- }
- }
- $post = Yii::$app->request->post();
- $id = $post['id'] ?? 0;
- $rejectReason = $post['rejectReason'] ?? '';
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- RefundOrderClass::rejectRefund($refund, $rejectReason);
- $transaction->commit();
- $staff = $this->shopAdmin;
- $staffId = $staff->id;
- $staffName = $staff->name ?? '';
- $refund->shopAdminId = $staffId;
- $refund->shopAdminName = $staffName;
- $refund->save();
- //审核结果通知
- $cgRefundId = $refund->cgRefundId ?? 0;
- $cgRefund = CgRefundClass::getById($cgRefundId, true);
- $cgShopId = $cgRefund->shopId ?? 0;
- $cgShop = ShopClass::getById($cgShopId, true);
- if (!empty($cgRefund) && !empty($cgShop)) {
- NoticeClass::afterSaleNotice($cgShop, $cgRefund);
- }
- util::complete('操作成功');
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("操作失败原因:" . $e->getMessage());
- util::fail('操作失败');
- }
- }
- //退款之后报损 ssh 20230409
- public function actionRefundWaste()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能报损');
- }
- $post = Yii::$app->request->post();
- $refundItemId = $post['refundItemId'] ?? 0;
- $wasteProductId = $post['wasteProductId'] ?? 0;
- $wasteNum = $post['wasteNum'] ?? 0;
- unset($post['refundId']);
- unset($post['wasteNum']);
- unset($post['wasteProductId']);
- $productInfo = ProductClass::getById($wasteProductId, true);
- if (empty($productInfo)) {
- util::fail('没有找到花材');
- }
- $ptItemId = $productInfo->itemId ?? 0;
- $ratio = $productInfo->ratio ?? 20;
- $refundItem = RefundOrderItemClass::getById($refundItemId, true);
- if (empty($refundItem)) {
- util::fail('没有找到退货信息');
- }
- if ($refundItem->mainId != $this->mainId) {
- util::fail('不是您的退款信息');
- }
- $unitType = $refundItem->xhUnitType ?? 0;
- if ($unitType == 1) {
- util::fail('无法报损');
- }
- $refundNum = $refundItem->xhNum ?? 0;
- $hasWasteNum = $refundItem->xhWasteNum ?? 0;
- $remainNum = bcsub($refundNum, $hasWasteNum);
- if ($wasteNum > $remainNum) {
- util::fail('剩余可报损数量 ' . $remainNum);
- }
- $productList = [['productId' => $wasteProductId, 'bigNum' => $wasteNum, 'smallNum' => 0, 'classId' => 0, 'itemId' => $ptItemId, 'ratio' => $ratio]];
- $post['shopId'] = $this->shopId;
- $post['sjId'] = $this->sjId;
- $post['mainId'] = $this->mainId;
- $post['shopAdminId'] = $this->shopAdminId;
- $shopAdmin = $this->shopAdmin;
- $adminName = $shopAdmin['name'] ?? '';
- $post['shopAdminName'] = $adminName;
- //属于售后报损
- $post['saleType'] = 1;
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
- $refundItem->save();
- $post['product'] = $productList;
- $return = StockWastageOrderClass::addOrder($post);
- $transaction->commit();
- util::success($return);
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::info("报损失败原因:" . $e->getMessage());
- util::fail('报损失败');
- }
- }
- //列表
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $this->mainId;
- $orderSn = $get['orderSn'] ?? '';
- if (!empty($orderSn)) {
- $where['relateOrderSn'] = $orderSn;
- }
- $searchTime = $get['searchTime'] ?? '';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $where['orderTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $refundSearchTime = $get['refundSearchTime'] ?? '';
- if (!empty($refundSearchTime)) {
- $startTime = $get['refundStartTime'] ?? '';
- $endTime = $get['refundEndTime'] ?? '';
- $period = dateUtil::formatTime($refundSearchTime, $startTime, $endTime);
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $customId = $get['customId'] ?? '';
- if (!empty($customId)) {
- $where['customId'] = $customId;
- }
- $shopAdminId = $get['shopAdminId'] ?? 0;
- if (!empty($shopAdminId)) {
- $where['shopAdminId'] = $shopAdminId;
- }
- $status = $get['status'] ?? -1;
- if ($status > -1) {
- $where['status'] = $status;
- }
- $export = $get['export'] ?? 0;
- if ($export == 1) {
- if (isset($where['orderTime']) == false && isset($where['addTime']) == false) {
- util::fail('请选时间');
- }
- ini_set('memory_limit', '2045M');
- set_time_limit(0);
- if (isset($where['orderTime'])) {
- $start = $where['orderTime'][1][0];
- $end = $where['orderTime'][1][1];
- $monthTime = bcmul(31, 86400);
- $startArea = strtotime($start);
- $endArea = strtotime($end);
- $diff = bcsub($endArea, $startArea);
- if ($diff > $monthTime) {
- util::fail('最长可导出一个月');
- }
- }
- if (isset($where['addTime'])) {
- $start = $where['addTime'][1][0];
- $end = $where['addTime'][1][1];
- $monthTime = bcmul(31, 86400);
- $startArea = strtotime($start);
- $endArea = strtotime($end);
- $diff = bcsub($endArea, $startArea);
- if ($diff > $monthTime) {
- util::fail('最长可导出一个月');
- }
- }
- }
- $respond = RefundOrderClass::getOrderList($where);
- if ($export == 1) {
- RefundOrderClass::exportRefundData($respond, $this->mainId);
- }
- util::success($respond);
- }
- //主要根据花材找退款记录 ssh 20240229
- public function actionGetListByItem()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $this->mainId;
- $status = $get['status'] ?? -1;
- $cause = $get['cause'] ?? -1;
- if ($status > -1) {
- $where['status'] = $status;
- }
- $string = $get['ids'];
- $ids = json_decode($string, true);
- if (!empty($ids)) {
- $productIds = [];
- $itemList = ProductClass::getAllByCondition(['mainId' => $this->mainId, 'classId' => ['in', $ids]], null, 'id', 'id');
- if (!empty($itemList)) {
- $productIds = array_keys($itemList);
- }
- if (empty($productIds)) {
- util::success(['list' => [], 'moreData' => 0, 'totalAmount' => 0, 'totalNum' => 0, 'totalPage' => 1]);
- }
- $where['productId'] = ['in', $productIds];
- }
- if ($cause > -1) {
- $where['cause'] = $cause;
- }
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $list = RefundOrderItemClass::getRefundItemList($where);
- util::success($list);
- }
- public function actionDetail()
- {
- $id = Yii::$app->request->get('id', 0);
- $refund = RefundOrderClass::getById($id);
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
- util::fail('没有权限');
- }
- $imgString = $refund['imgList'] ?? '';
- $relateOrderSn = $refund['relateOrderSn'] ?? '';
- $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
- if (empty($order)) {
- util::fail('数据出错了');
- }
- $refund['relateOrderId'] = $order->id;
- if (!empty($imgString)) {
- $imgArr = json_decode($imgString, true);
- $smallImgList = [];
- $bigImgList = [];
- if (!empty($imgArr)) {
- foreach ($imgArr as $image) {
- $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $smallImgList[] = $smallImg;
- $bigImgList[] = $bigImg;
- }
- }
- $refund['imgList'] = $imgArr;
- $refund['smallImgList'] = $smallImgList;
- $refund['bigImgList'] = $bigImgList;
- }
- $orderSn = $refund['orderSn'] ?? '';
- $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
- $staff = $this->shopAdmin;
- $staffId = $this->shopAdminId;
- $notify = NotifyClass::getByCondition(['staffId' => $staffId, 'type' => 0, 'targetId' => $id], true);
- if (!empty($notify)) {
- if ($notify->read == 0) {
- $notify->read = 1;
- $notify->save();
- $notifyNum = $staff->notifyNum;
- if ($notifyNum > 0) {
- $notifyNum--;
- $staff->notifyNum = $notifyNum;
- $staff->save();
- }
- }
- }
- $itemRefundOption = dict::getDict('itemRefundOption');
- util::success(['info' => $refund, 'itemList' => $itemList, 'itemRefundOption' => $itemRefundOption]);
- }
- //退款 lqh 2021.6.25
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
- util::fail('超管才能操作退款');
- }
- //多处要同步修改ghs_refund ssh 20250331
- if (getenv('YII_ENV') == 'production') {
- //小向花卉开单售后控制
- if ($this->mainId == 23390) {
- if (!in_array($this->adminId, [24043, 24585, 24586, 24654, 24115, 23960])) {
- util::fail('你不能售后哈。。');
- }
- //小向花卉售后必须备注
- $remark = $post['remark'] ?? '';
- if (empty($remark)) {
- util::fail('请填写备注007');
- }
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('无法操作。。');
- }
- }
- //小齐出车指定人可以售后
- if ($this->mainId == 42680) {
- if (!in_array($this->adminId, [40144])) {
- util::fail('你不能售后哦。。');
- }
- }
- }
- $id = isset($post['id']) ? $post['id'] : 0;
- $refundType = $post['refundType'] ?? 1;
- //避免网络延迟,暂时重复申请
- $cacheKey = 'ghs_order_refund_' . $id;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- util::fail('请5秒之后再提交');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
- //花材列表结构
- // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
- $productJson = $post['product'] ?? '';
- if (empty($productJson)) {
- util::fail('请选择花材');
- }
- $productList = json_decode($productJson, true);
- if (!is_array($productList)) {
- util::fail('请选择花材哦');
- }
- $post['product'] = $productList;
- } else {
- //仅退款花材直接设置为空
- $post['product'] = [];
- }
- $post['price'] = $post['price'] ?? 0;
- if ($post['price'] <= 0) {
- util::fail("退款金额不能小于0");
- }
- $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;
- $respond = OrderService::refund($id, $post);
- $refundId = $respond->id;
- $refundInfo = RefundOrderClass::getById($refundId, true);
- RefundOrderService::passRefund($refundInfo);
- $transaction->commit();
- util::success($respond);
- } catch (\Exception $exception) {
- $transaction->rollBack();
- Yii::info("退款出错了,报错信息:" . $exception->getMessage());
- util::fail('操作失败');
- }
- }
- //修改售后原因 ssh 20250623
- public function actionChangeRefundOption()
- {
- $post = Yii::$app->request->post();
- $id = $post['id'] ?? 0;
- $sonId = $post['sonId'] ?? 0;
- $optionId = $post['optionId'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- if (empty($refund)) {
- util::fail('没有找到退款信息');
- }
- if ($refund->mainId != $this->mainId) {
- util::fail('没有权限');
- }
- $orderSn = $refund->orderSn;
- $son = RefundOrderItemClass::getById($sonId, true);
- if (empty($son)) {
- util::fail('没有找到花材信息');
- }
- if ($son->orderSn != $orderSn) {
- util::fail('退款信息有问题');
- }
- $map = dict::getDict('itemRefundOption');
- $info = $map[$optionId] ?? [];
- $name = $info['name'] ?? '未选';
- $son->refundOptionId = $optionId;
- $son->refundOptionName = $name;
- $son->save();
- util::complete('修改成功');
- }
- }
|