CgRefundController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. NextDayRefundService::assertCanRefund($saleOrder);
  88. $forceNextDay = NextDayRefundService::mustUseNextDay($saleOrder);
  89. // 售后时限只用支付时间
  90. $payTime = $saleOrder->payTime;
  91. $currentTime = strtotime($payTime);
  92. $now = time();
  93. $saleHour = $ghsShop->saleHour;
  94. $saleHour = ceil($saleHour);
  95. if ($saleHour > 0) {
  96. $changeTime = $saleHour * 3600;
  97. $newTime = bcadd($currentTime, $changeTime);
  98. if ($now > $newTime) {
  99. util::fail('超时订单,请联系门店售后');
  100. }
  101. }
  102. //退款类型 1退货并退款 2 仅退款
  103. $refundType = $post['refundType'] ?? 1;
  104. if ($ghsShop->refundOnlyPrice == 0) {
  105. //不支持仅退款申请
  106. if ($refundType == 2) {
  107. util::fail('只能选 退货并退款');
  108. }
  109. }
  110. $imgList = $post['imgList'] ?? '';
  111. if ($ghsShop->refundMustImg == 1) {
  112. $imgList = trim($imgList);
  113. $arr = json_decode($imgList, true);
  114. if (empty($arr)) {
  115. util::fail('必须按规范提供照片');
  116. }
  117. }
  118. $connection = Yii::$app->db;
  119. $transaction = $connection->beginTransaction();
  120. try {
  121. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  122. //花材列表结构
  123. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  124. $productJson = $post['product'] ?? '';
  125. if (empty($productJson)) {
  126. util::fail('请选择要退款的花材');
  127. }
  128. $productList = json_decode($productJson, true);
  129. if (!is_array($productList)) {
  130. util::fail('请选择要退款的花材哦!');
  131. }
  132. $post['product'] = $productList;
  133. } else {
  134. //仅退款花材直接设置为空
  135. $post['product'] = [];
  136. }
  137. $post['price'] = $post['price'] ?? 0;
  138. if ($post['price'] <= 0) {
  139. util::fail("退款金额不能小于0");
  140. }
  141. // 隔天售后不减实付:上限用 actPrice - 已隔天累计;当天仍对比 realPrice
  142. if ($forceNextDay) {
  143. $remainNext = bcsub(
  144. (string)($saleOrder->actPrice ?? $cg->actPrice ?? '0'),
  145. (string)($saleOrder->nextDayTkPrice ?? $cg->nextDayTkPrice ?? '0'),
  146. 2
  147. );
  148. if (bccomp((string)$post['price'], $remainNext, 2) === 1) {
  149. util::fail('冲销金额超过可冲销余额');
  150. }
  151. } elseif (bccomp($post['price'], $cg->realPrice, 2) == 1) {
  152. util::fail("退款金额超过订单金额");
  153. }
  154. $post['shopId'] = $this->shopId;
  155. $post['sjId'] = $this->sjId;
  156. $post['shopAdminId'] = $this->shopAdminId;
  157. $post['mainId'] = $this->mainId;
  158. $shopAdmin = $this->shopAdmin;
  159. $adminName = $shopAdmin['name'] ?? '';
  160. $post['shopAdminName'] = $adminName;
  161. $respond = CgRefundService::createOrder($post, $cg);
  162. $transaction->commit();
  163. RefundOrderClass::printRefund($respond);
  164. $saleRefundId = $respond->saleRefundId ?? 0;
  165. $saleRefund = RefundOrderClass::getById($saleRefundId, true);
  166. if (!empty($saleRefund)) {
  167. NotifyClass::addRefundApplyNotify($saleRefund);
  168. $saleShopId = $saleRefund->shopId ?? 0;
  169. $saleShop = Shopclass::getById($saleShopId, true);
  170. if (!empty($saleShop)) {
  171. WxMessageClass::ghsHasNewRefundApplyInform($saleShop, $saleRefund);
  172. }
  173. }
  174. util::success($respond);
  175. } catch (\Exception $exception) {
  176. $transaction->rollBack();
  177. Yii::info("申请出错了,报错信息:" . $exception->getMessage());
  178. util::fail('申请失败');
  179. }
  180. }
  181. //列表
  182. public function actionList()
  183. {
  184. $get = Yii::$app->request->get();
  185. $where = [];
  186. $cgId = $get['cgId'] ?? 0;
  187. if (!empty($cgId)) {
  188. $where['cgId'] = $cgId;
  189. }
  190. $list = CgRefundClass::getRefundList($where);
  191. util::success($list);
  192. }
  193. //根据采购单查退款信息 ssh 20210908
  194. public function actionGetRefundData()
  195. {
  196. $id = Yii::$app->request->get('id', 0);
  197. $refund = CgRefundClass::getById($id);
  198. if (empty($refund)) {
  199. util::fail('没有找到退款信息');
  200. }
  201. $imgString = $refund['imgList'] ?? '';
  202. if (!empty($imgString)) {
  203. $imgArr = json_decode($imgString, true);
  204. $smallImgList = [];
  205. $bigImgList = [];
  206. if (!empty($imgArr)) {
  207. foreach ($imgArr as $image) {
  208. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  209. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  210. $smallImgList[] = $smallImg;
  211. $bigImgList[] = $bigImg;
  212. }
  213. }
  214. $refund['imgList'] = $imgArr;
  215. $refund['smallImgList'] = $smallImgList;
  216. $refund['bigImgList'] = $bigImgList;
  217. }
  218. //未登录用户也需要查看,暂时先注释
  219. //CgRefundClass::valid($refund, $this->mainId);
  220. $cgId = $refund['cgId'] ?? 0;
  221. $cg = PurchaseClass::getById($cgId);
  222. if (empty($refund)) {
  223. util::fail('没有退款信息');
  224. }
  225. // 详情「返回方式」优先用售后单资金快照;历史单无字段时回落采购原单
  226. $refundPayWay = $refund['payWay'] ?? ($refund->payWay ?? null);
  227. $refundOnlinePay = $refund['onlinePay'] ?? ($refund->onlinePay ?? null);
  228. if (($refundPayWay === null || $refundPayWay === '') && !empty($cg)) {
  229. $refund['payWay'] = is_array($cg) ? ($cg['payWay'] ?? 0) : ($cg->payWay ?? 0);
  230. }
  231. if (($refundOnlinePay === null || $refundOnlinePay === '') && !empty($cg)) {
  232. $refund['onlinePay'] = is_array($cg) ? ($cg['onlinePay'] ?? 0) : ($cg->onlinePay ?? 0);
  233. }
  234. $orderSn = $refund['orderSn'] ?? '';
  235. $cgItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  236. if (!empty($cgItemList)) {
  237. foreach ($cgItemList as $key => $item) {
  238. $cover = $item['cover'] ?? '';
  239. $shortCover = $cover;
  240. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  241. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  242. $cgItemList[$key]['cover'] = $cover;
  243. $cgItemList[$key]['bigCover'] = $bigCover;
  244. $cgItemList[$key]['shortCover'] = $shortCover;
  245. $cgItemList[$key]['itemNum'] = isset($item['itemNum']) ? floatval($item['itemNum']) : 0;
  246. }
  247. }
  248. util::success(['info' => $refund, 'itemList' => $cgItemList, 'cgInfo' => $cg]);
  249. }
  250. //根据退款id查退款信息 ssh 20210809
  251. public function actionGetInfo()
  252. {
  253. $id = Yii::$app->request->get('id', 0);
  254. }
  255. }