CgRefundController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace hd\controllers;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizGhs\notify\classes\NotifyClass;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizGhs\order\classes\RefundOrderClass;
  7. use bizGhs\order\services\NextDayRefundService;
  8. use bizHd\cg\classes\CgRefundClass;
  9. use bizHd\cg\classes\CgRefundItemClass;
  10. use bizHd\cg\services\CgRefundService;
  11. use bizHd\ghs\classes\GhsClass;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use bizHd\shop\classes\ShopClass;
  14. use common\components\imgUtil;
  15. use common\components\noticeUtil;
  16. use Yii;
  17. use common\components\util;
  18. class CgRefundController extends BaseController
  19. {
  20. public $guestAccess = ['list', 'get-refund-data'];
  21. //取消退款申请 ssh 20230808
  22. public function actionCancelRefund()
  23. {
  24. $get = Yii::$app->request->get();
  25. $id = isset($get['id']) ? $get['id'] : 0;
  26. $refund = CgRefundClass::getById($id, true);
  27. if (empty($refund)) {
  28. util::fail('没有找到退款单');
  29. }
  30. if ($refund->mainId != $this->mainId) {
  31. util::fail('无法操作');
  32. }
  33. $connection = Yii::$app->db;
  34. $transaction = $connection->beginTransaction();
  35. try {
  36. CgRefundService::cancelRefundApply($refund);
  37. $transaction->commit();
  38. util::complete('取消成功');
  39. } catch (\Exception $exception) {
  40. $transaction->rollBack();
  41. Yii::info("取消出错了,报错信息:" . $exception->getMessage());
  42. util::fail('取消失败');
  43. }
  44. }
  45. //申请退款 ssh 20230807
  46. public function actionCreateRefund()
  47. {
  48. $post = Yii::$app->request->post();
  49. $id = $post['id'] ?? 0;
  50. $cg = PurchaseClass::getById($id, true);
  51. if (empty($cg)) {
  52. util::fail('没有找到采购单');
  53. }
  54. if ($cg->mainId != $this->mainId) {
  55. util::fail('无法操作');
  56. }
  57. if (in_array($cg->debt, [0, 2]) && $cg->addTime < '2023-10-17 00:00:00') {
  58. util::fail('10月17日前订单无法申请退款');
  59. }
  60. $ghsId = $cg->ghsId ?? 0;
  61. $ghs = GhsClass::getById($ghsId, true);
  62. if (empty($ghs)) {
  63. util::fail('没有供货商信息');
  64. }
  65. $ghsShopId = $ghs->shopId ?? 0;
  66. $ghsShop = ShopClass::getById($ghsShopId, true);
  67. if (isset($ghsShop->afterSale) && $ghsShop->afterSale == 0) {
  68. util::fail('售后功能未开通');
  69. }
  70. if ($cg->status == PurchaseClass::STATUS_UN_PAY) {
  71. util::fail("待付款订单,不能申请售后");
  72. }
  73. if ($cg->status == PurchaseClass::STATUS_UN_SEND) {
  74. util::fail("待发货订单,不能申请售后");
  75. }
  76. if ($cg->status == PurchaseClass::STATUS_SENDING) {
  77. util::fail("订单配送中,不能申请售后");
  78. }
  79. if ($cg->status == PurchaseClass::STATUS_CANCEL) {
  80. util::fail("订单已取消,不能申请售后");
  81. }
  82. // 配对销售单:非当天/已结清走隔天售后,不再拦截「已结不能申请」
  83. $saleOrder = OrderClass::getById(intval($cg->saleId ?? 0), true);
  84. if (empty($saleOrder)) {
  85. util::fail('没有订单信息');
  86. }
  87. $forceNextDay = NextDayRefundService::mustUseNextDay($saleOrder);
  88. // 保留售后时限:限制花店随意申请(当天/隔天均校验)
  89. $payTime = $cg->payTime;
  90. $currentTime = strtotime($payTime);
  91. $now = time();
  92. $saleHour = $ghsShop->saleHour;
  93. $saleHour = ceil($saleHour);
  94. if ($saleHour > 0) {
  95. $changeTime = $saleHour * 3600;
  96. $newTime = bcadd($currentTime, $changeTime);
  97. if ($now > $newTime) {
  98. util::fail('超时订单,请联系门店售后');
  99. }
  100. }
  101. //退款类型 1退货并退款 2 仅退款
  102. $refundType = $post['refundType'] ?? 1;
  103. if ($ghsShop->refundOnlyPrice == 0) {
  104. //不支持仅退款申请
  105. if ($refundType == 2) {
  106. util::fail('只能选 退货并退款');
  107. }
  108. }
  109. $imgList = $post['imgList'] ?? '';
  110. if ($ghsShop->refundMustImg == 1) {
  111. $imgList = trim($imgList);
  112. $arr = json_decode($imgList, true);
  113. if (empty($arr)) {
  114. util::fail('必须按规范提供照片');
  115. }
  116. }
  117. $connection = Yii::$app->db;
  118. $transaction = $connection->beginTransaction();
  119. try {
  120. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  121. //花材列表结构
  122. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  123. $productJson = $post['product'] ?? '';
  124. if (empty($productJson)) {
  125. util::fail('请选择要退款的花材');
  126. }
  127. $productList = json_decode($productJson, true);
  128. if (!is_array($productList)) {
  129. util::fail('请选择要退款的花材哦!');
  130. }
  131. $post['product'] = $productList;
  132. } else {
  133. //仅退款花材直接设置为空
  134. $post['product'] = [];
  135. }
  136. $post['price'] = $post['price'] ?? 0;
  137. if ($post['price'] <= 0) {
  138. util::fail("退款金额不能小于0");
  139. }
  140. // 隔天冲销不减实付:上限用 actPrice - 已隔天累计;当天仍对比 realPrice
  141. if ($forceNextDay) {
  142. $remainNext = bcsub(
  143. (string)($saleOrder->actPrice ?? $cg->actPrice ?? '0'),
  144. (string)($saleOrder->nextDayTkPrice ?? $cg->nextDayTkPrice ?? '0'),
  145. 2
  146. );
  147. if (bccomp((string)$post['price'], $remainNext, 2) === 1) {
  148. util::fail('冲销金额超过可冲销余额');
  149. }
  150. } elseif (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  151. util::fail("退款金额超过订单金额");
  152. }
  153. $post['shopId'] = $this->shopId;
  154. $post['sjId'] = $this->sjId;
  155. $post['shopAdminId'] = $this->shopAdminId;
  156. $post['mainId'] = $this->mainId;
  157. $shopAdmin = $this->shopAdmin;
  158. $adminName = $shopAdmin['name'] ?? '';
  159. $post['shopAdminName'] = $adminName;
  160. $respond = CgRefundService::createOrder($post, $cg);
  161. $transaction->commit();
  162. RefundOrderClass::printRefund($respond);
  163. $saleRefundId = $respond->saleRefundId ?? 0;
  164. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  165. if (!empty($saleRefund)) {
  166. NotifyClass::addRefundApplyNotify($saleRefund);
  167. $saleShopId = $saleRefund->shopId ?? 0;
  168. $saleShop = Shopclass::getById($saleShopId, true);
  169. if (!empty($saleShop)) {
  170. WxMessageClass::ghsHasNewRefundApplyInform($saleShop, $saleRefund);
  171. }
  172. }
  173. util::success($respond);
  174. } catch (\Exception $exception) {
  175. $transaction->rollBack();
  176. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  177. util::fail('申请失败');
  178. }
  179. }
  180. //列表
  181. public function actionList()
  182. {
  183. $get = Yii::$app->request->get();
  184. $where = [];
  185. $cgId = $get['cgId'] ?? 0;
  186. if (!empty($cgId)) {
  187. $where['cgId'] = $cgId;
  188. }
  189. $list = CgRefundClass::getRefundList($where);
  190. util::success($list);
  191. }
  192. //根据采购单查退款信息 ssh 20210908
  193. public function actionGetRefundData()
  194. {
  195. $id = Yii::$app->request->get('id', 0);
  196. $refund = CgRefundClass::getById($id);
  197. if (empty($refund)) {
  198. util::fail('没有找到退款信息');
  199. }
  200. $imgString = $refund['imgList'] ?? '';
  201. if (!empty($imgString)) {
  202. $imgArr = json_decode($imgString, true);
  203. $smallImgList = [];
  204. $bigImgList = [];
  205. if (!empty($imgArr)) {
  206. foreach ($imgArr as $image) {
  207. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  208. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  209. $smallImgList[] = $smallImg;
  210. $bigImgList[] = $bigImg;
  211. }
  212. }
  213. $refund['imgList'] = $imgArr;
  214. $refund['smallImgList'] = $smallImgList;
  215. $refund['bigImgList'] = $bigImgList;
  216. }
  217. //未登录用户也需要查看,暂时先注释
  218. //CgRefundClass::valid($refund, $this->mainId);
  219. $cgId = $refund['cgId'] ?? 0;
  220. $cg = PurchaseClass::getById($cgId);
  221. if (empty($refund)) {
  222. util::fail('没有退款信息');
  223. }
  224. $orderSn = $refund['orderSn'] ?? '';
  225. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  226. if (!empty($cgItemList)) {
  227. foreach ($cgItemList as $key => $item) {
  228. $cover = $item['cover'] ?? '';
  229. $shortCover = $cover;
  230. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  231. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  232. $cgItemList[$key]['cover'] = $cover;
  233. $cgItemList[$key]['bigCover'] = $bigCover;
  234. $cgItemList[$key]['shortCover'] = $shortCover;
  235. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  236. }
  237. }
  238. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  239. }
  240. //根据退款id查退款信息 ssh 20210809
  241. public function actionGetInfo()
  242. {
  243. $id = Yii::$app->request->get('id', 0);
  244. }
  245. }