| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- //退款退货
- namespace ghs\controllers;
- use bizGhs\order\classes\RefundOrderItemClass;
- use bizGhs\order\services\RefundOrderService;
- use bizGhs\shop\classes\ShopClass;
- use bizHd\cg\classes\CgRefundClass;
- use bizHd\notice\classes\NoticeClass;
- use bizHd\purchase\classes\PurchaseClearClass;
- use common\components\imgUtil;
- 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 20230811
- public function actionPass()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('没有权限操作');
- }
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $refund = RefundOrderClass::getById($id, true);
- RefundOrderClass::valid($refund, $this->mainId);
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- RefundOrderService::passRefund($refund);
- $transaction->commit();
- $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('没有权限操作');
- }
- $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();
- $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;
- $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;
- }
- $list = RefundOrderClass::getOrderList($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'] ?? '';
- 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);
- util::success(['info' => $refund, 'itemList' => $itemList]);
- }
- //退款 lqh 2021.6.25
- public function actionCreateOrder()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
- util::fail('超管才能操作退款');
- }
- //国恋只有指定人有退款权限
- if ($this->mainId == 7704) {
- if (in_array($this->shopAdminId, [133727, 133545]) == false) {
- util::fail('没有权限操作');
- }
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $post = Yii::$app->request->post();
- $id = isset($post['id']) ? $post['id'] : 0;
- $order = OrderClass::getById($id, true);
- OrderClass::valid($order, $this->shopId);
- if ($order->status != OrderClass::ORDER_STATUS_COMPLETE) {
- util::fail("订单完成了才能发起退款");
- }
- $customId = $order->customId ?? 0;
- $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*', null, true);
- $orderIds = [];
- if (!empty($clearList)) {
- foreach ($clearList as $clear) {
- $saleStr = $clear->saleIds ?? '';
- $deadline = $clear->deadline ?? '';
- if ($deadline < date("Y-m-d H:i:s")) {
- $clear->status = 3;
- $clear->save();
- } else {
- if (!empty($saleStr)) {
- $current = explode(',', $saleStr);
- $orderIds = array_merge($orderIds, $current);
- }
- }
- }
- if (!empty($orderIds) && in_array($id, $orderIds)) {
- util::fail('此客户有结账单未完成');
- }
- }
- $addTime = $order->addTime ?? '';
- $current = time();
- if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
- util::fail('历史订单,不能再发起退款');
- }
- if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
- util::fail('历史订单,不能发起退款哦');
- }
- //退款类型 1退货并退款 2 仅退款
- $refundType = $post['refundType'] ?? 1;
- if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
- //花材列表结构
- // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 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");
- }
- if (bccomp($post['price'], $order['realPrice'], 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;
- $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('操作失败');
- }
- }
- }
|