CgRefundController.php 12 KB

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